【发布时间】:2011-08-29 01:45:42
【问题描述】:
我正在使用 subprocess 模块从 python 运行二进制文件。
为了捕获二进制文件产生的输出,我使用:
proc = subprocess.Popen (command_args, shell=False, stdout=subprocess.PIPE)
out = proc.communicate()[0]
#print the output of the child process to stdout
print (out)
它的作用是在进程完成执行后打印进程的输出。无论如何我可以在程序执行时将此输出打印到标准输出吗?我真的需要看看输出是什么,因为这些程序可以运行很长时间。
感谢您的帮助。
【问题讨论】:
-
没关系,伙计们,我想我找到了解决方案:
Basically you have 3 options: Use threading to read in another thread without blocking the main thread. select on stdout, stderr instead of communicate. This way you can read just when data is available and avoid blocking. Let a library solve this, twisted is a obvious choice. -
如果您找到其他答案,例如 *.com/questions/4585692/… ,请链接到它,而不是在您自己的问题的评论中复制答案。
-
很抱歉。我认为评论中的重点是*.com/questions/4585692/的精髓……下次我会记住这一点
标签: python subprocess stdout