I created two scripts, when one of the scripts executes the other script and tries to receive it's output in real time.
The script that tries to get the output:
import subprocess
p = subprocess.Popen("python .\\print.py", stdout=subprocess.PIPE)
print p.stdout.readline()
The second script:
import time
print "1se12d12d32d3223"
print "\r"
time.sleep(10000)
The problem is that I get the output only when the child process dies, even though the line was already written to stdout.
I tried the following:
All I'm trying to do, is read the first line of stdout of the child process while the process is still alive. How can I do that in python?
By default, Python buffers its output. Output is automatically flushed under some circumstances. E.g. when you run the second script in a terminal, you will see the output immediately.
You can explicitly flush the output by inserting a sys.stdout.flush()
before your sleep()
call.