get text from rogramms runn by subprocess.Popen immediatetly

Jeremiah Dodds jeremiah.dodds at gmail.com
Thu Apr 16 08:30:10 EDT 2009


On Thu, Apr 16, 2009 at 12:12 PM, Rüdiger Ranft <_rdi_ at web.de> wrote:

> 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.
>
> Beneath is a test which shows what I did
>
> TIA
> Rudi
>
> ----8<-------8<-------8<-- iodummy.cpp -8<-------8<---
> #include <iostream>
> #include <unistd.h>
>
> int main()
> {
>        for( int i = 0; i < 10; i++ )
>        {
>                std::cerr << i << std::endl;
>                sleep(2);
>        }
> }
>
> from subprocess import Popen, PIPE
> from time import sleep
>
> p = Popen('./iodummy',stdin=PIPE, stdout=PIPE, stderr=PIPE)
> sleep(3)
> # now I expect '0\n1\n' in stderr, but read() blocks until
> # the end of iodummy.
> print p.stderr.read()
> p.wait()
>
> --
> GPG encrypted mails preferred.
> GPG verschlüsselte Mails bevorzugt.
> ---> http://chaosradio.ccc.de/media/ds/ds085.pdf Seite 20 <----
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>

Depending on what exactly you're doing (like if you need to send info back
to the program you're spawning), the socket module might be of use. I use it
to communicate between Perl and Python for some stuff I have to do at work,
and it works out rather well.

Specifically, I've found sockets and JSON to work really nicely for
inter-language program communication.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20090416/1d5a276a/attachment.html>


More information about the Python-list mailing list