我的python代码生成子进程,它输出stdout和stderr消息。 我需要用不同的方式打印。在
我有以下代码来生成子进程并从中获取stdout结果。在
cmd = ["vsmake.exe", "-f"]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
for line in iter(p.stdout.readline, ''):
print line,
sys.stdout.flush()
pass
p.wait()
如何修改代码以检查子进程是否也通过stderr打印出消息?在
我需要在子进程打印出某些内容后立即打印出stderr和stdout。而且它是跨平台的实现,所以应该在Mac/Linux/PC上运行
独立完成这个平台的最简单方法是使用线程(不幸的是)。下面是一些示例代码:
所以基本上错误输出被重定向到
stderr
管道。在如果你需要更多的实时信息。我的意思是,当派生的进程将某些内容打印到
^{pr2}$stdout or
stderr`时,就立即打印行,然后可以执行以下操作:在这种情况下,每次向}。在
stdout
或stderr
写入一行时,将打印两个线程。参数type_pipe
只是在打印行时进行区分,以知道它们是来自stderr
还是{相关问题 更多 >
编程相关推荐