[IPython-dev] threadsafe widgets

Loper, Jackson jackson_loper at brown.edu
Fri Jan 9 17:59:39 EST 2015


Ah, the threading situation seems to be more diresome than I thought.  I
believe the following code is unsafe to run.

=== Problem code ===

import IPython.display
from IPython.html import widgets
import threading
from numpy.random import randint

isw1=widgets.IntSlider()
IPython.display.display(isw1)

isw2=widgets.IntSlider()
IPython.display.display(isw2)

def randoset(wid,N):
    for i in range(N):
        wid.value=randint(0,50)

t1=threading.Thread(target=randoset,args=[isw1,100])
t2=threading.Thread(target=randoset,args=[isw2,100])
t1.start()
t2.start()

This code produces malformed message errors for me in the jupyterhub
terminal output.

I believe this is because all widgets seem to share the same iopub_socket
and write to it whenever the user changes a traitlet.  And of course zmq
sockets aren't threadsafe.

=== A few potential solutions ===

1) "Quick" -- Wrap iopub_socket in a threadsafe wrapper.  Surround every
important function call in a mutex.
2) "Hintjens-approved" -- Give every single widget its own inproc PUB
socket.  We'd have to spin up an extra thread (or a greenlet in some
pre-extant thread?  I don't really understand what the different IPython
threads do...but I could find out...) with a SUB socket to collect them all
together and forward them all out on iopub_socket.
3) "Dirty" -- Wrap IPython.kernel.zmq.session.Session.send inside a mutex.
Are there use-cases where anybody besides Session actually does anything
with iopub_socket?

=== Motivation ===

I think there could be valid use-cases for widgets which display results
from polling external resources, e.g. widgets which keep track of EC2
instances, widgets which monitor IPython.parallel workerbees, etc.

=== Conclusion ===

I'd be happy to attack this problem, if you guys think it would be useful.
If so, let me know what would be a good general direction, and I'll put
together a pull request.

Keep warm!

Jackson Loper
Pattern Theory Group
Division of Applied Math
Brown University

PS. I love the new text editor feature in the ipython master branch!  My
days of vim-over-ssh aren't over yet (NERDTree and push-notification of
filechange detection are still too useful to me when logging on from
multiple machines) but I can see the end in sight!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ipython-dev/attachments/20150109/1084d54e/attachment.html>


More information about the IPython-dev mailing list