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

Popular posts from this blog

node.js - How to mock a third-party api calls in the backend -

node.js - Why do I get "SOCKS connection failed. Connection not allowed by ruleset" for some .onion sites? -

matlab - 0-by-1 sym - What do I need to change in order to get proper symbolic results? -