在子进程运行时拦截它的stdout

在子进程运行时拦截它的stdout,第1张

在子进程运行时拦截它的stdout

正如查尔斯已经提到的那样,问题正在缓冲。在为SNMPd编写一些模块时遇到了类似的问题,并通过用自动刷新版本替换stdout来解决了该问题。

我使用了以下代码,这些代码受ActiveState上的一些帖子启发:

class FlushFile(object):    """Write-only flushing wrapper for file-type objects."""    def __init__(self, f):        self.f = f    def write(self, x):        self.f.write(x)        self.f.flush()# Replace stdout with an automatically flushing versionsys.stdout = FlushFile(sys.__stdout__)


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5632138.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-16
下一篇2022-12-16

发表评论

登录后才能评论

评论列表(0条)

    保存