当我设置标准输出时调用 subprocess.call 挂起 [英] 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.
当我使用以下内容时
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.
我使用的是 python 2.6.1,无法使用 check_output
I am using python 2.6.1, and unable to use check_output
推荐答案
作为 规范说,
不要将 stdout=PIPE 或 stderr=PIPE 与此函数一起使用.由于当前进程没有读取管道,如果子进程生成足够的输出到管道以填满操作系统管道缓冲区,它可能会阻塞.
Do not use stdout=PIPE or stderr=PIPE with this function. 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.
你需要的是subprocess.Popen
:
proc = subprocess.Popen(["fetch.py", "--cookies=/tmp/tmp-cookies"],
stdout=PIPE, stderr=PIPE)
stdout, stderr = proc.communicate()
return stdout
(另外,subprocess.call
不返回进程对象,只返回退出状态)
(Also, subprocess.call
does not return the process object, only exit status)
这篇关于当我设置标准输出时调用 subprocess.call 挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!