I’m writing a python script that calls another python script (script2). I want to get the print statements from script2 during the process, not only at the end. I have this code:
command = ['python', '-i', script2]
process = subprocess.Popen(command,
stderr=subprocess.STDOUT,
stdout = subprocess.PIPE)
print "process started"
while process.poll() is None:
line = process.stdout.readline()
if not line:
break
print line
This code does indeed print the output to the screen, but the subprocess never ends.
However, changing command to command = ['python', script2] ends the subprocess, but only prints the output to the screen AFTER the subprocess ended.
How can I get the desired behaviour? Thanks on beforehand,
Roel
Try invoking your subprocess with
-u(and do not use-i). This will stop its output from being buffered. You don’t see any output until it ends because it writes all its output into a buffer, which isn’t flushed until it either fills up or the process finishes.