Search code examples
pythonsubprocessstdoutpopenpsutil

How to read from child's stdout while it still alive


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:

  • Using os.pipe as an argument to Popen and then reading from the "read" pipe
  • Using shell=True as Popen argument
  • Using bufsize=1 as Popen argument
  • Using stdout.read(), stdout.readlines(), stdout.read(1), etc...
  • I can't use the fcntl module to make the pipe "Not blocking", because I use Windows

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?


Solution

  • 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.