在Ipython笔记本中观察长期过程

在Ipython笔记本中观察长期过程,ipython,Ipython,目前,重新连接后,ipython笔记本无法打印运行内核的任何输出。我尝试了以下方法: import sys, time with open('foo.log','w') as sys.stdout: for i in range(5): print i time.sleep(1) 一切都很好,我可以通过tail-f foo.log看到这个过程,但最后整个过程崩溃了,我得到了以下错误: ERROR:tornado.general:Uncaught exception, cl

目前,重新连接后,ipython笔记本无法打印运行内核的任何输出。我尝试了以下方法:

import sys, time

with open('foo.log','w') as sys.stdout:
  for i in range(5):
    print i
    time.sleep(1)
一切都很好,我可以通过
tail-f foo.log
看到这个过程,但最后整个过程崩溃了,我得到了以下错误:

ERROR:tornado.general:Uncaught exception, closing connection.
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/zmq/eventloop/zmqstream.py", line 407, in _run_callback
    callback(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/tornado/stack_context.py", line 275, in null_wrapper
    return fn(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/ipykernel/kernelbase.py", line 260, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "/usr/local/lib/python2.7/dist-packages/ipykernel/kernelbase.py", line 218, in dispatch_shell
    sys.stdout.flush()
ValueError: I/O operation on closed file
ERROR:tornado.general:Uncaught exception, closing connection.
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/zmq/eventloop/zmqstream.py", line 433, in _handle_events
    self._handle_recv()
  File "/usr/lib/python2.7/dist-packages/zmq/eventloop/zmqstream.py", line 465, in _handle_recv
    self._run_callback(callback, msg)
  File "/usr/lib/python2.7/dist-packages/zmq/eventloop/zmqstream.py", line 407, in _run_callback
    callback(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/tornado/stack_context.py", line 275, in null_wrapper
    return fn(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/ipykernel/kernelbase.py", line 260, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "/usr/local/lib/python2.7/dist-packages/ipykernel/kernelbase.py", line 218, in dispatch_shell
    sys.stdout.flush()
ValueError: I/O operation on closed file
ERROR:tornado.application:Exception in callback None
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/tornado/ioloop.py", line 883, in start
    handler_func(fd_obj, events)
  File "/usr/local/lib/python2.7/dist-packages/tornado/stack_context.py", line 275, in null_wrapper
    return fn(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/zmq/eventloop/zmqstream.py", line 433, in _handle_events
    self._handle_recv()
  File "/usr/lib/python2.7/dist-packages/zmq/eventloop/zmqstream.py", line 465, in _handle_recv
    self._run_callback(callback, msg)
  File "/usr/lib/python2.7/dist-packages/zmq/eventloop/zmqstream.py", line 407, in _run_callback
    callback(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/tornado/stack_context.py", line 275, in null_wrapper
    return fn(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/ipykernel/kernelbase.py", line 260, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "/usr/local/lib/python2.7/dist-packages/ipykernel/kernelbase.py", line 218, in dispatch_shell
    sys.stdout.flush()
ValueError: I/O operation on closed file

我终于设法将jupyter的消息记录到文件中,并立即刷新内存:

class InstantLogger():
  def __init__(self, f):
    self.f = open(f,'w')
  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()
  def close(self):
    self.f.close()
我可以将其用作:

old_stdout = sys.stdout
sys.stdout = InstantLogger('file.log')
print ....
sys.stdout.close()
sys.stdout = old_stdout

我终于设法将jupyter的消息记录到文件中,并立即刷新内存:

class InstantLogger():
  def __init__(self, f):
    self.f = open(f,'w')
  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()
  def close(self):
    self.f.close()
我可以将其用作:

old_stdout = sys.stdout
sys.stdout = InstantLogger('file.log')
print ....
sys.stdout.close()
sys.stdout = old_stdout

也许你应该问stackoverflow,它更面向开发者。也许你应该问stackoverflow,它更面向开发者。