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

Doug Blank doug.blank at gmail.com
Sat Mar 22 10:01:58 EDT 2014


On Fri, Mar 21, 2014 at 7:44 PM, MinRK <benjaminrk at gmail.com> wrote:
>
>
>
> 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.

Ok, thanks for the feedback. I've opened issues #5408 to request this issue:

https://github.com/ipython/ipython/issues/5408

-Doug

> -MinRK
>
>>
>>
>> -Doug
>> _______________________________________________
>> IPython-dev mailing list
>> IPython-dev at scipy.org
>> http://mail.scipy.org/mailman/listinfo/ipython-dev
>
>
>
> _______________________________________________
> IPython-dev mailing list
> IPython-dev at scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-dev
>



More information about the IPython-dev mailing list