我有两个剧本。a.py,它打印b.py脚本的输出,如下所示:
#a.py
from subprocess import Popen, PIPE, STDOUT
p = Popen(['/Users/damian/Desktop/b.py'], shell=False, stdout=PIPE, stderr=STDOUT)
while p.poll() is None:
print p.stdout.readline()
#b.py
#!/usr/bin/env python
import time
while 1:
print 'some output'
#time.sleep(1)
这个很好。但是, 为什么在取消注释时脚本会死锁时间。睡觉()行?在
你的输出可能是缓冲的。为stdout添加一个.flush()以清除它:
如果将
-u
添加到a.py
中的调用(使输出无缓冲),则无需修改b.py
脚本:见more ways to get output from a subprocess。在
相关问题 更多 >
编程相关推荐