在Python的sys.stdout解释器中默认启用输出缓冲吗?

如果答案是肯定的,那么有哪些方法可以禁用它?

目前的建议:

使用-u命令行开关 包装系统。每次写入后刷新的对象中的标准输出 设置PYTHONUNBUFFERED env变量 sys。Stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)

是否有其他方法在sys/sys中设置全局标志。在执行期间以编程方式Stdout ?


如果只是想在使用打印的特定写入之后刷新,请参阅如何刷新打印函数的输出?。


当前回答

这与Cristóvão D. Sousa的回答有关,但我还不能评论。

使用Python 3的flush关键字参数以始终拥有未缓冲输出的直接方法是:

import functools
print = functools.partial(print, flush=True)

然后,print将始终直接刷新输出(除非flush=False给出)。

注意,(a)这只回答了部分问题,因为它没有重定向所有输出。但我猜打印是在python中创建输出到stdout/stderr的最常用方法,所以这两行可能涵盖了大多数用例。

注意(b)它只在定义它的模块/脚本中工作。这在编写模块时很好,因为它不会混淆sys.stdout。

Python 2不提供flush参数,但您可以模拟Python 3类型的打印函数,如此处所述https://stackoverflow.com/a/27991478/3734258。

其他回答

以下代码适用于Python 2.6、2.7和3.2:

import os
import sys
buf_arg = 0
if sys.version_info[0] == 3:
    os.environ['PYTHONUNBUFFERED'] = '1'
    buf_arg = 1
sys.stdout = os.fdopen(sys.stdout.fileno(), 'a+', buf_arg)
sys.stderr = os.fdopen(sys.stderr.fileno(), 'a+', buf_arg)

是的,默认启用。当调用python时,可以在命令行上使用-u选项禁用它。

在Python 3中,你可以修补打印函数,以始终发送flush=True:

_orig_print = print

def print(*args, **kwargs):
    _orig_print(*args, flush=True, **kwargs)

正如在评论中指出的,你可以通过functools.partial将flush形参绑定到一个值来简化这一点:

print = functools.partial(print, flush=True)
# reopen stdout file descriptor with write mode
# and 0 as the buffer size (unbuffered)
import io, os, sys
try:
    # Python 3, open as binary, then wrap in a TextIOWrapper with write-through.
    sys.stdout = io.TextIOWrapper(open(sys.stdout.fileno(), 'wb', 0), write_through=True)
    # If flushing on newlines is sufficient, as of 3.7 you can instead just call:
    # sys.stdout.reconfigure(line_buffering=True)
except TypeError:
    # Python 2
    sys.stdout = os.fdopen(sys.stdout.fileno(), 'w', 0)

工作人员:“塞巴斯蒂安”,在Python邮件列表的某个地方。

获得无缓冲输出的一种方法是使用sys。Stderr而不是sys。Stdout或简单地调用sys.stdout.flush()显式强制执行写操作。

你可以很容易地重定向打印的所有内容:

import sys; sys.stdout = sys.stderr
print "Hello World!"

或者为特定的打印语句重定向:

print >>sys.stderr, "Hello World!"

要重置stdout,你可以这样做:

sys.stdout = sys.__stdout__