I am running a script to control processes on a remote machine (SSH). Let me 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 now I run
ssh user@host five.py
I would like to see a conclusion
0 1 2 3 4
appears on my standard the second second (as if it were running locally). What happens: I immediately get 0 from the echo, and the rest appear immediately after the completion of the entire program. (It doesn't help to insert "five.py" in the bash script; call it "python five.py" or use "print -> sys.stdout, i").
This should be due to the way python writes to stdout, as other programs behave normally. Functional workaround
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 applications!
source share