bash - python stdout to file does not respond in real time -
write simple python script test.py:
import time print "begin" time.sleep(10) print "stop"
in bash, run
python test.py > log.txt
what observe both "begin" , "stop" appear in log.txt @ same time, after 10 seconds.
is expected behavior?
you need flush buffer after printing.
import sys import time print "begin" sys.stdout.flush() time.sleep(10) print "end" sys.stdout.flush()
or in python 3:
# can done in python 2.6+ # __future__ import print_function import time print("begin", flush=true) time.sleep(10) print("end", flush=true)
Comments
Post a Comment