get text from rogramms runn by subprocess.Popen immediatetly

Rüdiger Ranft _rdi_ at
Thu Apr 16 15:02:05 CEST 2009

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.


from subprocess import Popen, PIPE
from time import sleep

p = Popen('./iodummy',stdin=PIPE, stdout=PIPE, stderr=PIPE, bufsize=2)
# now I expect '0\n1\n' in stderr
print 'Point1:', p.communicate(None)
print 'Point2:', p.communicate(None)

Point1: ('', '0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n')
Traceback (most recent call last):
  File "", line 9, in ?
    print 'Point2:', p.communicate('')
  File "/usr/lib/python2.4/", line 1028, in communicate
ValueError: I/O operation on closed file

GPG encrypted mails preferred.
GPG verschlüsselte Mails bevorzugt.
---> Seite 20 <----

More information about the Python-list mailing list