get text from rogramms runn by subprocess.Popen immediatetly

Diez B. Roggisch deets at
Thu Apr 16 15:45:01 CEST 2009

Rüdiger Ranft schrieb:
> Diez B. Roggisch schrieb:
>> Rüdiger Ranft schrieb:
>>> Hi all,
>>> I need to call some programms and catch their stdout and stderr streams.
>>> While the Popen class from subprocess handles the call, I get the
>>> results of the programm not until the programm finishes. Since the
>>> output of the programm is used to generate a progress indicator, I need
>>> a way to acces the values written to stdout/stderr as fast as possible.
>> Use the communicate()-method of Popen-objects.
> It gives the same behavior, the first call to communicate gets all text,
> the second gives a closed handle error :(. I also tried
> p.communicate(''), which gives the same as p.communicate(None) result.

That's likely due to buffering then. You could try & see if flushing on 
the side of the subprocess works (you can't do that from python's side 
of things within this scenario).

Or you might want to use pexpect that emulates a pseudo-terminal which 
also changes the way data is displayed.


More information about the Python-list mailing list