为了澄清一些答案。
例如,这将等待8秒,打印一行,再等待5秒钟,打印第二行。
from time import sleep
def test():
print "ok",
time.sleep(3)
print "Now",
time.sleep(5)
print "done"
time.sleep(5)
print "again"
test()
为ruby
,STDOUT.sync =true
打开autoflush
;所有写入STDOUT
后跟flush()
。这样可以解决您的问题,但会导致更多的IO。
STDOUT.sync = true
对于python
,您可以使用python -u
或环境变量PYTHONUNBUFFERED
来使它stdin/stdout/stout
不被缓冲,但是还有其他解决方案不会改变stdin
或stderr
export PYTHONUNBUFFERED=1
对于perl
,你有autoflush
autoflush STDOUT 1;