output to console and to multiple files

Fuzzyman fuzzyman at gmail.com
Fri Feb 16 19:52:06 EST 2007


On Feb 16, 11:37 pm, "nathan.sh... at gmail.com" <nathan.sh... at gmail.com>
wrote:
> On Feb 16, 4:07 pm, garri... at gmail.com wrote:
>
>
>
> > On Feb 16, 3:28 pm, "Gabriel Genellina" <gagsl... at yahoo.com.ar> wrote:
>
> > > That's ok inside the same process, but the OP needs to use it "from a
> > > subprocess or spawn".
> > > You have to use something like tee, working with real file handles.
>
> > I'm not particularly familiar with this, but it seems to me that if
> > you're trying to catch stdout/stderr from a program you can call with
> > (say) popen2, you could just read from the returned stdout/stderr
> > pipe, and then write to a series of file handles (including
> > sys.stdout).
>
> > Or am I missing something? =)
>
> > ~G
>
> That works, but it isn't live streaming of stdout/stderr. Most of the
> time, if you stream both, one could lock the process, or have the
> stdout/stderr printed in the wrong order.

Everytime I've looked to do something like this (non-blocking read on
the stdout of a subprocess) I've always come back to the conclusion
that threads and queues are the only reasonable way (particularly on
windows). There may be a better solution using select.

Fuzzyman
http://www.voidspace.org.uk/python/articles.shtml




More information about the Python-list mailing list