Flushing stdout

G

Graham Ashton

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,
 
S

Steve Holden

Graham said:
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.
You aren't actually having trouble flushing sys.stdout at all ...
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.
Nope. The holdup is elsewhere.
Any thoughts? Thanks.

P.S. I've not found -u and PYTHONUNBUFFERED to help me.
Nope, they won't ...
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,
The iteration over the pipe's contents is waiting until it sees the end
of the output before it yields anything. Try this:

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])
f = os.popen(command, "r", 1)
while 1:
line = f.readline()
if not line:
break
print "received", line,

You can probably find a tidier way to bundle this now you know what's up.

regards
Steve
 
G

Graham Ashton

You aren't actually having trouble flushing sys.stdout at all ...


The iteration over the pipe's contents is waiting until it sees the end
of the output before it yields anything.

Genius. Thanks Steve, much appreciated. Now I can actually send our users
some feedback...

Graham
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top