Synchronous capture of stdout and stderr

Neil Hodgson nhodgson at bigpond.net.au
Fri Aug 30 19:59:36 EDT 2002


Konrad Wojas wrote:

> I'm working on a build tool that captures the stdout and stderr of an 
> application and mails the output if a compile fails. I'm now using 
> popen2.popen3 and the select module, but I don't receive the two streams in 
> the same order as the application sends them.

    A common problem here is that the application buffers the streams 
when writing to a pipe but not when writing to a terminal. Using a pty 
instead may convince the child application to run unbuffered. Is there a 
flag (like Python's -u) to run the application in unbuffered mode?

    While this may improve matters, there is a further problem in that 
you are running on a time sharing OS that is unlikely to switch from the 
  child application to your application after every output. So by the 
time  your application is activated there may be output available on 
both streams and you have no way of discovering the ordering of the two. 
There may be several pieces of data available on either stream and there 
is no way of discovering the ordering relationship of each byte between 
the streams. You could lower the priority of the child application to 
try to ensure that your application is activated whenever there is 
anything to read but I doubt the scheduler guarantees to switch after 
every stream write.

    Neil




More information about the Python-list mailing list