跳到主要内容
版本:0.3.x

StdoutWrapper

私有上下文源码

with syncstream.webtools.StdoutWrapper():
...

用来确保stdout总是重定向到同一位置的封装。

危险

该类是从零实现的。然而,它的功能已经完全被contextlib.redirect_stdout(...)contextlib.redirect_stdout(...)覆盖了。因此,用户不再应该使用该上下文。

该文档只是用作备案,以记录该实现完成了哪些工作。

参数

该类不提供初始化参数。

范例

将stdout重定向到缓存

use_stdout_context.py
import sys
import syncstream
from syncstream.webtools import StdoutWrapper

buffer = syncstream.LineBuffer(10)
with StdoutWrapper():
sys.stdout = buffer
for i in range(20):
print(f'Message "{i:02d}".')
print('No line break.', end='')

messages = buffer.read()
for mitem in messages:
print(mitem)