The output of the python scripts is only displayed when SSH is used?

advertisements

I'm running a script to manage processes on a remote (SSH) machine. Let's call it five.py

#!/usr/bin/python
import time, subprocess

subprocess.call('echo 0',shell=True)
for i in range(1,5):
   time.sleep(1)
   print(i)

If i now run

ssh [email protected] five.py

I would like to see the output

0
1
2
3
4

appear on my standard out second by second (as it does if execute locally).. What happens is: I get the 0 from "echo" right away and the rest only appears at once after the entire program finishes. (Doesn't help to nest 'five.py' into a bash script; to call it by 'python five.py'; or to use 'print >> sys.stdout, i').

This must be related to the way python writes to stdout, since other programs behave quite normal.. A functional workaround is

import time, subprocess
import sys

subprocess.call('echo 0',shell=True)
for i in range(1,5):
  time.sleep(1)
  sys.stdout.write(str(i)+'\n')
  sys.stdout.flush()

But there must be a better solution than changing all my print statements!


You can add the -u on the shebang line as interjay hinted

#!/usr/bin/python -u

You could also reopen stdout with buffering turned off or set to line buffering

import os,sys
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0) # no buffering
sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 1) # line buffering

Usually line buffering is a good choice