本文介绍了有没有一种方法可以将stderr重定向到Jupyter中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

IPython.utils中有一个redirect_output函数,还有一个%% capture魔术函数,但是现在这些函数已经消失了,并且现在已过时.

There was a redirect_output function in IPython.utils, and there was a %%capture magic function, but these are now gone, and this thread on the topic is now outdated.

我想做以下事情:

from IPython.utils import io
from __future__ import print_function
with io.redirect_output(stdout=False, stderr="stderr_test.txt"):
    while True:
        print('hello!', file=sys.stderr)

有什么想法吗?为了获得更多的上下文,我试图捕获运行数小时或数天的某些ML函数的输出,并每5-10秒向stderr输出一行.然后,我想获取输出,对其进行调整,然后绘制数据.

Thoughts? For more context, I am trying to capture the output of some ML functions that run for hours or days, and output a line every 5-10 seconds to stderr. I then want to take the output, munge it, and plot the data.

推荐答案

@Ben,仅替换sys.stderr不起作用,并且完全刷新逻辑.但是感谢您的指导,因为它最终给了我一个可行的版本:

@Ben, just replacing sys.stderr did not work, and the full flush logic suggested in the post was necessary. But thank you for the pointer as it finally gave me a working version:

import sys
oldstderr = sys.stderr
sys.stderr = open('log.txt', 'w')
class flushfile():
    def __init__(self, f):
        self.f = f
    def __getattr__(self,name): 
        return object.__getattribute__(self.f, name)
    def write(self, x):
        self.f.write(x)
        self.f.flush()
    def flush(self):
        self.f.flush()
sys.sterr = flushfile(sys.stderr)
from __future__ import print_function
# some long running function here, e.g. 
for i in range(1000000):
    print('hello!', file=sys.stderr)
sys.stderr = oldstderr

如果Jupyter保留redirect_output()功能和/或%%capture魔术,那就太好了.

It would have been nice if Jupyter kept the redirect_output() function and/or the %%capture magic.

这篇关于有没有一种方法可以将stderr重定向到Jupyter中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 20:35