python – Popen stdout读取管道,使用睡眠死锁

好吧,我有两个脚本. 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)

这很有效.但是,
当我取消注释time.sleep()行时,为什么我的脚本会死锁?

最佳答案 您的输出可能已缓冲.添加
.flush() for stdout以清除它:

import sys
import time

while 1:
    print 'someoutput'
    sys.stdout.flush()
    time.sleep(1)
点赞