【问题标题】:Calling 2 external applications at the same time同时调用 2 个外部应用程序
【发布时间】:2017-10-08 11:24:54
【问题描述】:

我有一个 python 脚本,我试图在其中同时调用它们。 我把它写成:

os.system('externalize {0}'.format(result))
os.system('viewer {0} -b {1}'.format(img_list[0], img_list[1]))

但是这样做,第二个应用程序只会打开/出现,除非我退出/退出第一个应用程序。

我尝试如下使用subprocess

subprocess.call('externalize {0}'.format(result), shell=True)
subprocess.call('viewer {0} -b {1}'.format(img_list[0], img_list[1]))

但我并没有取得多大的成功。我在某处做错了吗?

【问题讨论】:

  • 你想做什么?只是同时运行两个命令?或者让他们以某种方式互动?
  • 如您所见,我得到了 2 个不同的输出 - resultimg_list。在我的代码中,我这样写,一旦收集了这两个应用程序,两个应用程序 - externalizeviewer 将一起运行

标签: python subprocess


【解决方案1】:

Run them as subprocesses without waiting for finish:

p1=subprocess.Popen(<args1>)
p2=subprocess.Popen(<args2>)

如果/当您需要等待它们完成和/或检查它们的退出代码时,请对这些对象调用 wait()(或 whatever else applicable)。

(一般来说,you should never ignore the object that Popen() returns and its exit code if you need to do something as a result of the subprocess' work(例如,如果它们是临时的,则清理您提供给他们的文件)。)

【讨论】:

    【解决方案2】:

    几个subprocess 函数,如call 只是异步执行程序的Popen 对象的便利包装。你可以改用它

    将子进程导入为子进程

    结果 = 'foo' img_list = ['bar', 'baz']

    proc1 = subp.Popen('externalize {0}'.format(result), shell=True)
    proc2 = subp.Popen('viewer {0} -b {1}'.format(img_list[0], img_list[1]), shell=True)
    proc1.wait()
    proc2.wait()
    

    【讨论】:

      猜你喜欢
      • 2013-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-28
      • 1970-01-01
      • 2011-11-10
      • 2016-07-22
      相关资源
      最近更新 更多