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

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.


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


  • GPU Memory bandwidth theoretical vs practical
  • Check precision to nth decimal place?
  • Why is bash so painfully slow in boot2docker for Windows?
  • How to implement interleaved page allocation in a user-mode NUMA-aware memory allocator?
  • Spinner in Chrome, IE9 does not become visible during synchronous ajax get request
  • No projects found to import
  • Prevent focus to URL bar with CTRL + L
  • how can i get floating imgs to fill all space
  • Windows batch string manipulation in loop
  • Sending rails errors to rspec output
  • Retrieving values from a PHP Multi-dimensional Array
  • Prolog Ambiguous Output
  • Why cout is producing no output on Code Blocks?
  • Cross platform UI spacing/padding
  • Why doesnt this Java loop in a thread work?
  • Best practice to eliminate magic numbers within a member function
  • How to plot large time series (thousands of administration times/doses of a medication)?
  • import scipy.sparse failed
  • Memory error in python- how to use more memory
  • During installation of Django, why do I keep getting ImportError: No module named django?
  • where do I find the xml.dom python package for the python-2.6.0-8.9.28 and I have a suse/x86_64 vers
  • Python pickle not one-to-one: different pickles give same object
  • SAVE attribute needed for Fortran variables when only the C_LOC address is returned to a C program?
  • Differences in dis-assembled C code of GCC and Borland?
  • why xml file does not aligned properly after append the string in beginning and end of the file usin
  • Date Conversion from yyyy-mm-dd to dd-mm-yyyy
  • Debug.DrawLine not showing in the GameView
  • Textfile Structure (tables)
  • Unity3D & Android: Difference between “UnityMain” and “main” threads?
  • Alert pop up with LWUIT
  • Python CGI os.system causing malformed header
  • Algorithm for a smudge tool?
  • R - Combining Columns to String Based on Logical Match
  • Read text file and split every line in MSBuild
  • Knitr HTML Loop - Some HTML output, some R output
  • Jquery - Jquery Wysiwyg return html as a string
  • Return words with double consecutive letters
  • Confusion with PayPal's monthly billing cycle
  • how to add data labels for bar graph in matlab
  • Checking variable from a different class in C#