473,396 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Flushing stdout

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,

Jul 18 '05 #1
2 3834
Graham Ashton wrote:
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
--
http://www.holdenweb.com
http://pydish.holdenweb.com
Holden Web LLC +1 800 494 3119
Jul 18 '05 #2
On Mon, 01 Nov 2004 16:41:04 -0500, Steve Holden wrote:
Graham Ashton wrote:
Hi. I'm having trouble flushing sys.stdout.

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


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
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Farshid Lashkari | last post by:
Hi, My application has python embedded into it. I noticed that when I run any python code the output is buffered and doesn't get flushed until my application exits. To fix this I simply flush...
3
by: gf gf | last post by:
Is there any way to make Python's print() flush automatically, similar to...mmm...that other language's $|=1 ? If not, how can I flush it manually? sys.stdout.flush() didn't seem to work. ...
7
by: Mike | last post by:
I would like my 'print' statements to send its output to the user's screen and a log file. This is my initial attempt: class StdoutLog(file): def __init__(self, stdout,...
3
by: Robert Smith | last post by:
I have some printf statements in my code and they arnt printing out. They only print out with \n on the end. That tells me i need to flush the buffer. But i dont know how. I am using gcc and...
6
by: Mazze | last post by:
Hi. I have a strange problem ... I want to create a process and capture stdout : ... gint std_out; char* argv = {"/opt/sun-jdk-1.4.2.08/bin/java","helloworld",NULL}; //char* argv =...
0
by: theJade | last post by:
I'm currently running Python 2.5 on Windows 2003. I'm not sure if this is strictly a python issue but I was wondering if anyone has seen and/or knows how to fix this problem. From a linux...
6
by: Rajesh S R | last post by:
Consider the following code: #include <stdio.h> int main( void ) { printf("Hello"); fflush(stdout); return 0; }
0
by: Ethan Metsger | last post by:
Hi, all. I apologize for what is perhaps a newb question. I'm in the process of transitioning our testing framework from Perl to Python. While that alone probably sets off some red flags, I'm...
4
by: lovecreatesbea... | last post by:
For example, in Bourne Shell both stdout and stderr can be re-directed to /dev/null, $ ./a.out 2>&1 /dev/null then is there any difference still?
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.