get text from rogramms runn by subprocess.Popen immediatetly
Rüdiger Ranft
_rdi_ at web.de
Thu Apr 16 09:02:05 EDT 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.
bye
Rudi
cat run.py
from subprocess import Popen, PIPE
from time import sleep
p = Popen('./iodummy',stdin=PIPE, stdout=PIPE, stderr=PIPE, bufsize=2)
sleep(3)
# now I expect '0\n1\n' in stderr
print 'Point1:', p.communicate(None)
sleep(3)
print 'Point2:', p.communicate(None)
p.wait()
python run.py
Point1: ('', '0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n')
Point2:
Traceback (most recent call last):
File "run.py", line 9, in ?
print 'Point2:', p.communicate('')
File "/usr/lib/python2.4/subprocess.py", line 1028, in communicate
self.stdin.flush()
ValueError: I/O operation on closed file
--
GPG encrypted mails preferred.
GPG verschlüsselte Mails bevorzugt.
---> http://chaosradio.ccc.de/media/ds/ds085.pdf Seite 20 <----
More information about the Python-list
mailing list