简体   繁体   English

设置标准输出时,调用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. 我有一个我称之为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

As the spec says, 正如规范所说,

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.

相关问题 在python中使用subprocess.call时如何将stdout重定向到文件? - How do I redirect stdout to a file when using subprocess.call in python? Python:从subprocess.call捕获stdout - Python: Capture stdout from subprocess.call 如何获取subprocess.call的stdout和stderr? - How to get the stdout and stderr for subprocess.call? subprocess.call无法将stdout发送到ffmpeg - subprocess.call can not send stdout to ffmpeg 当使用subprocess.call()时,如果使用非必需配置进行日志记录,如何设置它以将stdout重定向到日志文件的方式? - When using subprocess.call(), how to set it in way that redirects stdout to a log file if you are using the disctionary configuration for logging? 调用 subprocess.call(['.', bash_path]) 时权限被拒绝 - Permission denied when calling subprocess.call(['.', bash_path]) 在另一个目录的.exe上调用subprocess.call() - Calling subprocess.call() on a .exe in another directory 从python subprocess.call调用rsync - calling rsync from python subprocess.call 使用参数从subprocess.call调用app - Calling app from subprocess.call with arguments subprocess.call() 在将 stdout 重定向到文件时抛出错误“FileNotFoundError: [Errno 2] No such file or directory” - subprocess.call() throws error "FileNotFoundError: [Errno 2] No such file or directory" when redirecting stdout to file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM