reading stdout from child

Alex Martelli alex at magenta.com
Wed Jul 26 08:46:01 EDT 2000


"Thomas Thiele" <thiele at muc.das-werk.de> wrote in message
news:397ECD5D.409A6819 at muc.das-werk.de...
    [snip]
You may be running into *buffering* problems.  Python, like most languages,
will, by default, *buffer* its output when it is directed to a file (or
pipe); i.e.,
each 'written' datum is actually only added to a memory 'buffer', and the
whole
buffer gets _really_ written out, in one go, when it's filling up, or when
the file
is closed, or when the program is terminating.  This is really crucial to
enhance
program performance, but it does sometimes interfere with programs that
want to read each other's output.

Fortunately, Python gives you the easiest way to defeat output buffering, of
any language I know of: just use the -u switch (option) on the command line.
I.e., instead of
    python something.py
run
    python -u something.py
and all output will become unbuffered.  See if it works this way: it's a
very
cheap and easy experiment!  If it does work, then this confirms it's a
buffering
problem.  You can then, if you wish, attack it more directly (without having
to completely disable the buffering functionality) by explicitly *flushing*
the
output-streams at strategical points in your program -- this "flushing" will
cause
the memory-buffer to be truly "written out" as and when you request that.


Alex






More information about the Python-list mailing list