1

我有两个文件:

运行.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)

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

4

1 回答 1

2

主脚本的输出被缓冲。sys.stdout.flush()在运行子进程之前调用。

于 2013-08-06T02:23:56.147 回答