I am using subprocess module for running commands from another app I know that you are able to do the following
import subprocess
app = subprocess(args, stdout=subprocess.PIPE)
out, err = app.communicate()
print out
I would like the output to be displayed as it happening as supposed to one big blob at the end. Ideas?
The problem may be that the command you are running in the subprocess buffers its output: in which case, in Blender’s answer, the
process.stdout.read(1)would not return until the output buffer of the subprocess filled up, causing it to be flushed, and thus seen by the parent process.See this answer and this one for more information about how buffering affects things.
Here’s some scripts to illustrate:
and
Now, put both scripts in the same directory and run
python runspew.py. You’ll see something like this:Now, remove the comment from
sys.stdout.flush()inspew.pyand dopython runspew.pyagain. You’ll now see something like this:Notice that
runspew.pydidn’t change: only the program which was run as the subprocess did. If the program you’re running can’t be made to run unbuffered (or flush frequently), then you’ll have to useexpect/unbufferas described in the other answers I linked to.