[IPython-dev] Output from background threads in notebook?

MinRK benjaminrk at gmail.com
Fri Mar 21 19:44:51 EDT 2014

On Sun, Mar 16, 2014 at 6:30 PM, Doug Blank <doug.blank at gmail.com> wrote:

> If you run the following cell:
> import threading
> import time
> class ThreadClass(threading.Thread):
>     def run(self):
>         x = 1
>         while True:
>             print(x)
>             x += 1
>             time.sleep(1)
> t = ThreadClass()
> t.start()
> t.join()
> Then you will see the data for as long as you don't interrupt the
> cell. If you comment out the t.join(), then you won't see any output
> (or maybe just the first printed).
> In the next cell, if you do something such as, say:
> while True:
>     pass
> and then interrupt the kernel, you'll see some more of the printouts.
> It appears that when the execution_reply is received, the notebook
> doesn't take output messages anymore. Is this all by design? Is there
> a manner to make sure background-running output can still make its way
> to the notebook?

zmq sockets are not threadsafe. To protect against segfaults, background
threads are not allowed to write directly to the IOPub socket. What they do
instead is write to a buffer, which the main thread checks periodically.
That's why async output from a thread doesn't show up until there is some
interaction in the main thread.

It would be easy enough to hook up that checking / flushing to the main
eventloop, in which case it would still be threadsafe, but still allow
publication while the kernel is idle. But that's now how it works right now.


> -Doug
> _______________________________________________
> IPython-dev mailing list
> IPython-dev at scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-dev
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20140321/d4923afc/attachment.html>

More information about the IPython-dev mailing list