How to catch a line with Popen

Dan Stromberg drsalists at gmail.com
Sat May 28 20:37:19 EDT 2011


On Sat, May 28, 2011 at 4:32 PM, Tim Roberts <timr at probo.com> wrote:

> TheSaint <nobody at nowhere.net.no> wrote:
> >
> >I'm looking into subprocess.Popen docs.
> >I've launch the program with its arguments and that's smooth. I'm
> expecting
> >to read the output by *comunicate()* at every line that prgram may blow
> >during the process, but the output is given only when the child process is
> >ended.
> >I'd like to process the lines to display an information in percentage
> during
> >the running time of the child.
>
> Are you specifying a buffer size in the Popen command?  If not, then the
> Python side of things is unbuffered.  It's possible that the application
> you are launching is buffering its output, so that it doesn't SEND until
> the application ends.
>

I believe there are 4 normal ways, and 1 special way of getting output from
a buffered application:

1) As you mentioned, the program could terminate
2) Also, the program could just have written enough to fill its buffer, so
it outputs the content of the buffer before starting to fill it again
3) Also, the program could call flush
4) Also, the program could use an unbuffered file, like stderr - not all
file in an application are astonishingly likely to be buffered the same way

And the special one:
1) Sometimes you can take a buffered program and get it to be unbuffered (or
at least line buffered) by running it on a pty.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110528/835d9c97/attachment.html>


More information about the Python-list mailing list