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

Watch long term process in Ipython notebook

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

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