Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Capturing stderr and stdout of a subprocess as a single stream

Reply
Thread Tools

Capturing stderr and stdout of a subprocess as a single stream

 
 
Fuzzyman
Guest
Posts: n/a
 
      01-07-2007
Hello all,

Before I ask the question a couple of notes :

* This question is for implementing a script inside the Wing IDE. For
some reason using the subprocess module doesn't work so I need a
solution that doesn't use this module.
* The platform is Windows and I'm happy with a Windoze only solution.


I would like to execute subprocesses asynchronously and capture stdout
/ stderr as a single stream.

If I use "os.popen3(executable)" it gives me separate pipes for stdout
and stderr, but reads from them are blocking.

The output on stdout and stderr may be interleaved and I would like to
display them *as* they arrive. That means I can't just read from them
and output the results.

The only solution I can think of is to read from both a character at a
time on two separate threads, putting the data into a queue. A separate
thread could pull characters off the queue and display them. (I don't
need to differentiate between stdout and stderr when I display.)

Can anyone think of a better solution ?

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

 
Reply With Quote
 
 
 
 
Gabriel Genellina
Guest
Posts: n/a
 
      01-07-2007


On 7 ene, 16:33, "Fuzzyman" <fuzzy...@gmail.com> wrote:
> Hello all,
>
> Before I ask the question a couple of notes :
>
> * This question is for implementing a script inside the Wing IDE. For
> some reason using the subprocess module doesn't work so I need a
> solution that doesn't use this module.
> * The platform is Windows and I'm happy with a Windoze only solution.
>
>
> I would like to execute subprocesses asynchronously and capture stdout
> / stderr as a single stream.
>
> If I use "os.popen3(executable)" it gives me separate pipes for stdout
> and stderr, but reads from them are blocking.
>
> The output on stdout and stderr may be interleaved and I would like to
> display them *as* they arrive. That means I can't just read from them
> and output the results.
>
> The only solution I can think of is to read from both a character at a
> time on two separate threads, putting the data into a queue. A separate
> thread could pull characters off the queue and display them. (I don't
> need to differentiate between stdout and stderr when I display.)
>
> Can anyone think of a better solution ?
>
> 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,
>
> Fuzzymanhttp://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).

--
Gabriel Genellina

 
Reply With Quote
 
 
 
 
Fuzzyman
Guest
Posts: n/a
 
      01-07-2007

Gabriel Genellina wrote:
> On 7 ene, 16:33, "Fuzzyman" <fuzzy...@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


 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      01-07-2007
On 7 ene, 17:37, "Fuzzyman" <fuzzy...@gmail.com> wrote:

> Two threads and a queue sounds horrible.


But unfortunately it's the only way if you don't control how the child
process behaves.
(It's not soooo ugly afterwards... certainly would be worse if you had
to syncronize both reading threads and the display thread using
semaphores by hand.)

--
Gabriel Genellina

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
subprocess.Popen and ordering writes to stdout and stderr Chris Withers Python 1 12-18-2009 06:57 PM
capturing stdout and stderr? Mike Perl Misc 0 08-09-2009 09:33 PM
Correctly reading stdout/stderr from subprocess Christoph Haas Python 0 06-13-2006 10:20 PM
Joining stdout & stderr of subprocess ? robert Python 4 04-21-2006 05:04 PM
sys.stdout / sys.stderr, subprocess, redirection Roman Neuhauser Python 0 04-04-2005 08:57 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57