Capturing stderr and stdout of a subprocess as a single stream

Fuzzyman fuzzyman at gmail.com
Sun Jan 7 21:37:07 CET 2007


Gabriel Genellina wrote:
> On 7 ene, 16:33, "Fuzzyman" <fuzzy... at gmail.com> wrote:
[snip..]
> > My current code works, but *doesn't* capture stderr :
> >
> > from threading import Thread
> >
> > pipe = os.popen(executable)
> >
> > def DisplayOutput():
> >     while True:
> >         output = pipe.read(1)
> >         if not output:
> >             break
> >         display(output)
> >
> > Thread(target=DisplayOutput).start()
> >
> > All the best,
> >
> > Fuzzyman
> > http://www.voidspace.org.uk/python/articles.shtml
>
> Try using popen4 instead. But since you already have a thread, it may
> be better to use popen2 and two threads to read from stdout and stderr
> to avoid a potential deadlock; they can put read lines into a Queue,
> and DisplayOutput just get these lines in order. (See the warnings in
> the popen2 module documentation).
>

popen4 works great, I didn't even know it existed.

Two threads and a queue sounds horrible.

Thanks

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

> -- 
> Gabriel Genellina




More information about the Python-list mailing list