我试图用subprocess.call在Python中运行外部应用程序.从我读过的内容来看,除非你调用Popen.wait,否则subprocess.call不应该阻塞,但对我来说它是阻塞的,直到外部应用程序退出.我该如何解决?
你正在阅读错误的文档.根据他们:
subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False)
Run Code Online (Sandbox Code Playgroud)
运行args描述的命令.等待命令完成,然后返回returncode属性.
中的代码subprocess实际上非常简单且可读。只需查看3.3或2.7版本(视情况而定),您就可以知道它在做什么。
例如,call看起来像这样:
def call(*popenargs, timeout=None, **kwargs):
"""Run command with arguments. Wait for command to complete or
timeout, then return the returncode attribute.
The arguments are the same as for the Popen constructor. Example:
retcode = call(["ls", "-l"])
"""
with Popen(*popenargs, **kwargs) as p:
try:
return p.wait(timeout=timeout)
except:
p.kill()
p.wait()
raise
Run Code Online (Sandbox Code Playgroud)
您无需调用即可执行相同的操作wait。创建一个Popen,不要调用wait它,这正是您想要的。