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

Ах, и оба тройника и перенаправление в файл делают буфер не линейным, а просто работают с tty flushes на каждой строке. Благодаря! – theicfire