Line buffering stdout fails on MINGW / MSYS Python 2.7.3

The problem is illustrated with 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 second gap between printing the two lines. If not, both lines will be displayed almost simultaneously after a 10 second pause (after starting Python); that is, the lines are output when the program exits.

On Linux, I see line and buffer behavior for both file and screen if the "sys.stdout" line is included. Without this line, I see behavior on the buffered line on the screen, but not on the file. Expected.

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

What's weird is that with the "sys.stdout" line, I can't see the line bindings to the screen or file. I expect to see this in both Linux and 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.

+2


source to share


1 answer


One of my colleagues knew the answer.

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



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

+2


source







All Articles