Flushing stdout

Graham Ashton gashton at cmedltd.com
Mon Nov 1 16:34:53 EST 2004


Hi. I'm having trouble flushing sys.stdout. I've written a small example
to illustrate my problem (see below). In short, I expect it to ping
"received hello", sleep for 5 seconds and then print "received world".
Instead I get nothing for 5 seconds and then both statements pop out at
once.

As you'll no doubt gather from the example, the problem I'm really trying
to solve is sending message back from one process to another via a pipe.
Changing the buffering option in the os.pipe() call doesn't have any
effect either.

Any thoughts? Thanks.

P.S. I've not found -u and PYTHONUNBUFFERED to help me.

P.P.S. I'm running 2.3.3 on Linux, libc 2.2.5-11.5.

---cut---
#!/usr/bin/env python

import os, sys, time

if len(sys.argv) > 1:
    sys.stdout.write("hello\n")
    sys.stdout.flush()
    time.sleep(5)
    sys.stdout.write("world\n")
else:
    command = "python %s foo" % os.path.basename(sys.argv[0])
    for line in os.popen(command, "r", 1):
        print "received", line,




More information about the Python-list mailing list