在MINGW/MSYS Python 2.7.3上,stdout的行缓冲失败 [英] 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'
如果行缓冲成功,则两行打印之间将有10秒的间隔.如果不是这样,则在暂停10秒后(Python启动后),这两行几乎同时出现.也就是说,程序退出时将打印这些行.
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.
在Linux上,如果包含"sys.stdout"行,则在文件和屏幕上都会看到行缓冲行为.如果没有该行,我会在屏幕上看到行缓冲的行为,但在文件中却看不到.这是预期的.
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.
在MSYS/MINGW环境中,如果省略"sys.stdout"行,则会看到与Linux相同的行为:行缓冲到屏幕而不是文件.
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.
奇怪的是,带有"sys.stdout"行,我看不到 屏幕或文件的行缓冲.我希望两者都能看到,就像在Linux中一样.
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?
这里有更多信息:
uname -a MINGW32_NT-6.0 FOO 1.0.11(0.46/3/2)2009-05-23 19:33 i686 Msys
uname -a MINGW32_NT-6.0 FOO 1.0.11(0.46/3/2) 2009-05-23 19:33 i686 Msys
谢谢, -W.
推荐答案
我的一位同事知道答案.
One of my colleagues knew the answer.
在WIN32上不支持行缓冲.如果指定了行缓冲,它将恢复为完整缓冲.提供了无缓冲的输出,解决方法是在WIN32上使用它.我已经在简单的测试程序中对其进行了尝试,并且可以正常工作.
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.
引用: http://msdn .microsoft.com/en-us/library/86cebhfs%28v = vs.71%29.aspx
这篇关于在MINGW/MSYS Python 2.7.3上,stdout的行缓冲失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!