[英]Python subprocess.call blocking
I am trying to run an external application in Python with subprocess.call. 我试图用subprocess.call在Python中运行外部应用程序。 From what I've read it subprocess.call isn't supposed to block unless you call Popen.wait, but for me it is blocking until the external application exits. 从我读过的内容来看,除非你调用Popen.wait,否则subprocess.call不应该阻塞,但对我来说它是阻塞的,直到外部应用程序退出。 How do I fix this? 我该如何解决?
You're reading the docs wrong. 你正在阅读错误的文档。 According to them: 根据他们:
subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False)
Run the command described by args. 运行args描述的命令。 Wait for command to complete, then return the returncode attribute. 等待命令完成,然后返回returncode属性。
The code in subprocess
is actually pretty simple and readable. subprocess
的代码实际上非常简单易读。 Just see the 3.3 or 2.7 version (as appropriate) and you can tell what it's doing. 只需看看3.3或2.7版本(视情况而定),您就可以知道它在做什么。
For example, call
looks like this: 例如, 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
You can do the same thing without calling wait
. 你可以在不叫wait
的情况下做同样的事情。 Create a Popen
, don't call wait
on it, and that's exactly what you want. 创建一个Popen
,不要wait
它,这正是你想要的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.