2013-08-06 98 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

相關問題