[英]Calling subprocess.call hangs when I set the stdout
I have a function which I call a progarm, with some args and want to get the result. 我有一个我称之为progarm的函数,带有一些args,想要得到结果。
When I use the following 当我使用以下
proc = subprocess.call(["fetch.py", "--cookies=/tmp/tmp-cookies"], stdout=PIPE, stderr=PIPE)
stdout, stderr = proc.communicate()
return stdout
The app just hangs. 该应用程序只是挂起。 But if I run
但是如果我跑步
return subprocess.call(["fetch.py", "--cookies=/tmp/tmp-cookies"])
then I get the output on my screen and the app works fine, however I need to get the output into a function. 然后将输出显示在屏幕上,应用程序运行正常,但是我需要将输出转换为函数。
I am using python 2.6.1, and unable to use check_output 我正在使用python 2.6.1,并且无法使用check_output
Do not use stdout=PIPE or stderr=PIPE with this function.
请勿将此功能与stdout = PIPE或stderr = PIPE一起使用。 As the pipes are not being read in the current process, the child process may block if it generates enough output to a pipe to fill up the OS pipe buffer.
由于在当前进程中未读取管道,因此子进程如果向管道生成足够的输出以填满OS管道缓冲区,则子进程可能会阻塞。
What you need instead, is subprocess.Popen
: 相反,您需要的是
subprocess.Popen
:
proc = subprocess.Popen(["fetch.py", "--cookies=/tmp/tmp-cookies"],
stdout=PIPE, stderr=PIPE)
stdout, stderr = proc.communicate()
return stdout
(Also, subprocess.call
does not return the process object, only exit status) (此外,
subprocess.call
不会返回流程对象,只会退出状态)
Your subprocess must read its stdin before the communication is complete, and the main process can continue. 在通信完成之前,您的子进程必须先读取其stdin,并且主进程可以继续。 The workaround would be write out in a thread.
解决方法将在线程中写出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.