[英]subprocess: Can communicate() timeout, given that wait() terminates?
Is it possible for Popen.communicate(timeout=2)
to raise TimeoutExpired
, even though I am asserting that Popen.poll() is not None
and Popen.wait(2)
does not throw an exception? 有可能是
Popen.communicate(timeout=2)
以提高TimeoutExpired
,即使我断言Popen.poll() is not None
和Popen.wait(2)
不抛出异常?
Edit: The docs suggest to use the following snippet: 编辑: 文档建议使用以下代码段:
proc = subprocess.Popen(...)
try:
outs, errs = proc.communicate(timeout=15)
except TimeoutExpired:
proc.kill()
outs, errs = proc.communicate()
but this will just raise ProcessLookupError: [Errno 3] No such process
. 但这只会引发
ProcessLookupError: [Errno 3] No such process
。 Which makes sense because I've already terminated the process via poll
and wait
. 这是有道理的,因为我已经通过
poll
和wait
终止了该过程。
Yes. 是。 It is possible if the child spawns its own subprocesses.
子代可能会生成自己的子流程。
Popen.communicate()
may return much later than Popen.wait()
which waits only for the child. Popen.communicate()
可能远远晚于返回Popen.wait()
只为孩子等待。 See Python subprocess' check_call()
vs check_output()
. 参见Python子
check_call()
与check_output()
。
Note: proc.kill()
might not kill the whole process tree. 注意:
proc.kill()
可能不会杀死整个进程树。 See How to terminate a python subprocess launched with shell=True
. 请参阅如何终止由
shell=True
启动的python子进程 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.