简体   繁体   English

子进程:鉴于wait()终止,communication()是否可以超时?

[英]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 NonePopen.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 . 这是有道理的,因为我已经通过pollwait终止了该过程。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM