Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python stdout to file does not respond in real time

Write a simple python script test.py:

  import time
  print "begin"
  time.sleep(10)
  print "stop"

In bash, run

python test.py > log.txt

What I observe is that both "begin" and "stop" appear in log.txt at the same time, after 10 seconds.

Is this expected behavior?

like image 970
Ruofeng Avatar asked Dec 24 '22 21:12

Ruofeng


1 Answers

Have you tried calling python with the -u option, which "forces stdin, stdout and stderr to be totally unbuffered" =>

python -u test.py > log.txt
like image 187
Julien Spronck Avatar answered Dec 27 '22 10:12

Julien Spronck