Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output of python scripts displayed only at termination when using SSH?

Tags:

python

ssh

stdout

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 user@host 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!

like image 662
Ivan Lappo-Danilevski Avatar asked Feb 25 '10 17:02

Ivan Lappo-Danilevski


1 Answers

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

like image 163
John La Rooy Avatar answered Nov 07 '22 17:11

John La Rooy