How do I control output buffering on Win32?

Grant Edwards grante at visi.com
Fri Aug 15 11:10:41 EDT 2008


On 2008-08-15, Fredrik Lundh <fredrik at pythonware.com> wrote:
> 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            



More information about the Python-list mailing list