By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,920 Members | 1,767 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,920 IT Pros & Developers. It's quick & easy.

How do I control output buffering on Win32?

P: n/a
When I ssh in to my Windows XP box and run Python apps, output
from "print" and and "sys.stdout.write()" is being buffered so
that none of the output shows up until the program exits.

From within my program how do I set output buffering to either
line-buffered or un-buffered? [I'm looking for the equivalent
of the C stdio "setbuf" call.]

--
Grant Edwards grante Yow! I'm rated PG-34!!
at
visi.com
Aug 15 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Grant Edwards wrote:
When I ssh in to my Windows XP box and run Python apps, output
from "print" and and "sys.stdout.write()" is being buffered so
that none of the output shows up until the program exits.
>>From within my program how do I set output buffering to either
line-buffered or un-buffered? [I'm looking for the equivalent
of the C stdio "setbuf" call.]
I'm not sure you can do that from within the program [1]; to do it from
the outside, set the PYTHONUNBUFFERED environment variable to something,
or pass "-u" to the interpreter.

</F>

1) well, I suppose you could replace sys.stdout with a thin wrapper that
does flush after each write, or play with os.fdopen.

Aug 15 '08 #2

P: n/a
On 2008-08-15, Fredrik Lundh <fr*****@pythonware.comwrote:
Grant Edwards wrote:
>When I ssh in to my Windows XP box and run Python apps, output
from "print" and and "sys.stdout.write()" is being buffered so
that none of the output shows up until the program exits.
>>>From within my program how do I set output buffering to either
line-buffered or un-buffered? [I'm looking for the equivalent
of the C stdio "setbuf" call.]

I'm not sure you can do that from within the program [1]; to
do it from the outside, set the PYTHONUNBUFFERED environment
variable to something, or pass "-u" to the interpreter.
I had seen references to PYTHONUNBUFFERED in my Google search,
but it wasn't mentioned when I did a "python -h" -- so I
assumed it was a Unix-only thing.
1) well, I suppose you could replace sys.stdout with a thin
wrapper that does flush after each write, or play with
os.fdopen.
I thought about that, but it seemed like there ought to be an
easier, more obvious way.

Is there some technical reason a Python program can't control
the output buffering the way a C program can with setvbuf()? Or
is it just something that's only wanted by weird people who do
things like ssh into Windows boxes?

That raises another question: what is the criteria that Python
is using when it decides to run my program in block-buffered
mode instead of line- or un-buffered mode?

--
Grant Edwards grante Yow! I've read SEVEN
at MILLION books!!
visi.com
Aug 15 '08 #3

P: n/a
On 2008-08-15, Grant Edwards <gr****@visi.comwrote:
>I'm not sure you can do that from within the program [1]; to
do it from the outside, set the PYTHONUNBUFFERED environment
variable to something, or pass "-u" to the interpreter.

I had seen references to PYTHONUNBUFFERED in my Google search,
but it wasn't mentioned when I did a "python -h"
assumed it was a Unix-only thing.
Doh. Yes it was. I don't know how I missed it...

--
Grant Edwards grante Yow! Uh-oh!! I forgot
at to submit to COMPULSORY
visi.com URINALYSIS!
Aug 15 '08 #4

P: n/a
On Fri, 15 Aug 2008 09:47:34 -0500, Grant Edwards wrote:
When I ssh in to my Windows XP box and run Python apps, output
from "print" and and "sys.stdout.write()" is being buffered so
that none of the output shows up until the program exits.

From within my program how do I set output buffering to either
line-buffered or un-buffered? [I'm looking for the equivalent
of the C stdio "setbuf" call.]
That's not exactly what you want, but what about creating your
own printing function and flushing at its end?

import sys
def printf(mystr):
print mystr
sys.stdout.flush()

--
Regards,
Wojtek Walczak,
http://www.stud.umk.pl/~wojtekwa/
Aug 15 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.