I am working on a cluster and using Python to do some calculations.
However, python scripts does not print anything until they finish or get killed.
I have checked some ways (from Disable output buffering)
- Use the
-u
command line switch - Wrap
sys.stdout
in an object that flushes after every write - Set
PYTHONUNBUFFERED
env var sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)
But none of these works.
The python version is 2.7.8, 2.7.3 and 2.4.2 (There are three versions on the system, which must be put into PATH before running)
Also, because the cluster uses NFS, I have the exactly the same python among multiple machines. But on one machine, it prints unbuffered (at least it prints something before ending), without any setting at all (no PYTHONUNBUFFERD). On other machines it does not, which makes me even more confusing.
My testing script is
import os,sys,time
for i in range(100):
print(i)
time.sleep(1)
And
import os,sys,time
sys.stdout = os.fdopen(sys.stdout.fileno(),'w',0)
for i in range(100):
print(i)
time.sleep(1)
And
import os,sys,time
for i in range(100):
print(i)
sys.stdout.flush()
time.sleep(1)
Is there any other way to make Python print unbuffered (or buffered more reasonably, like buffer for one line) without modifing Python codes (because there are a lot of them)? Thanks.