Synchronous capture of stdout and stderr

Donn Cave donn at u.washington.edu
Tue Sep 3 12:37:57 EDT 2002


Quoth Konrad Wojas <usenet at wojas.vvtp.tudelft.nl>:
| Neil Hodgson wrote:
| > 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?
|
| No, the applications are make and gcc :)
|
| I guess I'll have to use popen4 and don't distinguish between stdout and 
| stderr. Maybe I could try writing a LD_PRELOAD wrapper some time.

I wouldn't expect make and gcc to buffer output like that.  Personally,
a single output stream sounds better to me anyway in this case, because
I don't recognize any great semantic distinction between standard and
error outputs in a make job.

But in answer to your immediate question, I would suggest two things -
use the UNIX file descriptor, file.fileno(), and the posix module read
and write functions, to avoid unwanted buffering, and secondly, use
the select.select() function to find output that's ready to read.

	Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list