[英]Python Subprocess communicate fails after terminate
I'm experiencing an issue where a call to proc.communicate()
still hangs even after calling proc.terminate()
. 我遇到一个问题,即调用
proc.communicate()
甚至要求后仍然挂起proc.terminate()
I create tasks to be run in the background with the call 我创建任务以在后台运行
import subprocess as sub
p = sub.Popen(command, stdout=sub.PIPE, stderr=sub.PIPE, shell=False)
At the completion of the script, I call terminate()
, communicate()
, and wait()
to gather information from the process. 在脚本完成时,我调用
terminate()
, communicate()
和wait()
来从进程中收集信息。
p.terminate()
errorcode = p.wait()
(pout, perr) = p.communicate()
The script hangs at the call to communicate. 脚本挂在通话处以进行通信。 I'd assumed that any call to communicate that follows a call to terminate would return immediately.
我假设在终止呼叫之后进行的任何通信呼叫都会立即返回。 Is there any reason why this would fail?
有什么原因会失败吗?
Edit: I'm using this method because the command is really a tight loop that won't terminate on its own. 编辑:我正在使用此方法,因为该命令实际上是一个紧密的循环,不会自行终止。 I'd like to use p.terminate() to do that, and then see what the stdout and stderr has to offer.
我想使用p.terminate()来执行此操作,然后查看stdout和stderr必须提供什么。
You don't need the first two statements. 您不需要前两个语句。 Just call
communicate()
. 只需调用
communicate()
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.