2013-08-06 102 views
1

我有两个文件:Python标准输出和子

run.py

import subprocess 
import time 

while True: 
    time.sleep(1) 
    print 'hello' 
    proc = subprocess.call(['./writer.sh']) 

writer.sh(CHMOD 777'd)

#!/bin/sh 
echo 'write something here' 

,我很困惑通过以下输出:

$ python run.py 
hello 
write something here 
hello 
write something here 
hello 
write something here 
.... 

$ python run.py | tee out.log 
write something here 
write something here 
(hello disappears) 

.... 

$ python run.py > out.log 
# Nothing, but out.log has the following: 
write something here 
write something here 
write something here 
write something here 
hello 
hello 
hello 
hello 
... # and the two basically "expand" the longer I run this (instead of appending) 

什么我发生什么事了,我怎样才能像第一个命令一样输出所有内容?

回答

2

您的主脚本的输出被缓冲。在运行子进程之前,请致电sys.stdout.flush()

+0

啊,并且tee和重定向到一个文件使得缓冲区不是一行一行,而只是在每一行上运行一次tty。谢谢! – theicfire

相关问题