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

元气小坏坏 提交于 2019-12-12 18:19:39

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/14346400/line-buffering-of-stdout-fails-on-mingw-msys-python-2-7-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!