473,569 Members | 2,799 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 PYTHONUNBUFFERE D 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.writ e("hello\n")
sys.stdout.flus h()
time.sleep(5)
sys.stdout.writ e("world\n")
else:
command = "python %s foo" % os.path.basenam e(sys.argv[0])
for line in os.popen(comman d, "r", 1):
print "received", line,

Jul 18 '05 #1
2 3841
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 PYTHONUNBUFFERE D 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.writ e("hello\n")
sys.stdout.flus h()
time.sleep(5)
sys.stdout.writ e("world\n")
else:
command = "python %s foo" % os.path.basenam e(sys.argv[0])
for line in os.popen(comman d, "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.writ e("hello\n")
sys.stdout.flus h()
time.sleep(5)
sys.stdout.writ e("world\n")
else:
command = "python %s foo" % os.path.basenam e(sys.argv[0])
f = os.popen(comman d, "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
4429
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 sys.stdout and sys.stderr every once in while by using the following code: //Get handle to python stdout file and flush it PyObject *pyStdout =...
3
9036
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
3965
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, name='/tmp/stdout.log', mode='w',bufsize=-1): super(StdoutLog, self).__init__(name,mode,bufsize) self.stdout = stdout
3
1769
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 linux, dont know if that matters
6
2554
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 = {"/usr/bin/du","/",NULL}; g_spawn_async_with_pipes("/work/", argv, NULL, G_SPAWN_CHILD_INHERITS_STDIN,
0
1940
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 system using an xterm terminal type, I telnet into the Windows box and execute 'python script.py'. script.py is simply a script that takes in user input...
6
80564
by: Rajesh S R | last post by:
Consider the following code: #include <stdio.h> int main( void ) { printf("Hello"); fflush(stdout); return 0; }
0
1358
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 afraid it's what I'm stuck with. I'm modeling a test with five operations: build, execute, validate, publish, and clean. The main loop might...
4
2285
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
7697
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8120
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7672
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6283
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1212
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.