逐行读取子过程标准输出
#fake_utility.py, just generates lots of output over timeimport time i = 0while True: print hex(i)*512 i += 1 time.sleep(0.5)#filters outputimport subprocess proc = subprocess.Popen(['python','fake_utility.py'],stdout=subprocess.PIPE)for line in proc.stdout: #the real code does filtering here print "test:", line.rstrip()
tee
最新情况:
sys.stdout.flush()
proc.stdout.xreadlines()
更新2:
#fake_utility.py, just generates lots of output over timeimport sys, timefor i in range(10): print i sys.stdout.flush() time.sleep(0.5)#display out put line by lineimport subprocess proc = subprocess.Popen(['python','fake_utility.py'],stdout=subprocess.PIPE)#works in python 3.0+ #for line in proc.stdout:for line in iter(proc.stdout.readline,''): print line.rstrip()
烙印99
相关分类