Python脚本没有实时print信息

test.py:

import time

while 1:

  print 'a'

  time.sleep(1)

执行python脚本,python test.py > output

然后用tailf output查看,却没有内容

google了下,是因为stdout默认是buffered I/O,所以是print到缓冲区了

有几种办法可以解决这个问题:

print之后加sys.stdout.flush()

或者python -u test.py,-u参数是强制stdin,stdout,stderr无缓冲

原文地址:https://www.cnblogs.com/justff/p/3234918.html