Line-buffering of stdout fails on MINGW/MSYS Python 2.7.3
Asked Answered
D

1

4

The problem is illustrated by this simple script:

import time, os, sys
sys.stdout = os.fdopen( sys.stdout.fileno(), 'w', 1 ) # line-buffer stdout
print 'before sleep'
time.sleep( 10 )
print 'after sleep'

If line-buffering is successful, then there will be a 10-sec gap between the printing of the two lines. If not, both lines will appear virtually at the same time after a 10-sec pause (once Python starts up); that is, the lines are printed when the program exits.

On Linux, I see line-buffered behavior to both a file and to the screen if the "sys.stdout" line is included. Without that line, I see line-buffered behavior to the screen, but not to a file. This is expected.

In the MSYS/MINGW environment, if I omit the "sys.stdout" line, I see the same behavior as Linux: line-buffering to the screen but not to a file.

What is weird is that with the "sys.stdout" line, I don't see line-buffering to either the screen or a file. I expect to see it to both, as in Linux.

Can anyone suggest a workaround?

Here's a bit more information:

uname -a MINGW32_NT-6.0 FOO 1.0.11(0.46/3/2) 2009-05-23 19:33 i686 Msys

Thanks, -W.

Desiredesirea answered 15/1, 2013 at 20:35 Comment(0)
D
5

One of my colleagues knew the answer.

Line buffering is not supported on WIN32. If line buffering is specified, it reverts to full buffering. Unbuffered output is available, and the workaround is to use it on WIN32. I have tried it in my simple test program, and it works.

Ref.: http://msdn.microsoft.com/en-us/library/86cebhfs%28v=vs.71%29.aspx

Desiredesirea answered 16/1, 2013 at 15:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.