You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Multiprocessing usage results in ZMQError: Address already in use #6109
Some custom python code that uses multiprocessing was occasionally throwing ZMQErrors for me when used in a notebook.
Here's a simple test case I made that always reproduces the issue for me. It creates a process that creates a process.
from multiprocessing import Process
def worker_worker():
d = Process(target=worker)
d.start()
d.join()
def worker():
pass
p = Process(target=worker_worker)
p.start()
p.join()
Running this as a script with python, it finishes correctly. Running it in an ipython notebook causes a hang. When interrupted the exception shown is:
Exception in thread Thread-4:
Traceback (most recent call last):
File "/usr/local/Cellar/python/2.7.7_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 810, in __bootstrap_inner
self.run()
File "/Users/rkhadiwala/Library/Python/2.7/lib/python/site-packages/zmq/utils/garbage.py", line 34, in run
s.bind(self.gc.url)
File "socket.pyx", line 444, in zmq.backend.cython.socket.Socket.bind (zmq/backend/cython/socket.c:4076)
File "checkrc.pxd", line 21, in zmq.backend.cython.checkrc._check_rc (zmq/backend/cython/socket.c:6032)
ZMQError: Address already in use
system information (I've also tried this on another box (linux) and saw the same issue):
I'm seeing the same issue when running multiprocessing code from the ipython-qtconsole. I have a similar system to you, but I'm running Anaconda with python 2.7.8
I think the problem here is that multiprocessing on Unix uses fork() by default to start the worker processes. Mixing forking (used by multiprocessing) and threading (used by IPython/ZMQ) is not necessarily supposed to work in the first place (see e.g. this bug). It's possible that the ZMQError is due to there being two copies of ZMQ trying to connect to the same port after the fork.
before importing the example in the IPython notebook makes it work fine.
It may be possible to make IPython "fork-safe" -- for example, the original discussion in this bug was about making the logging module work with fork -- but I don't know what this would involve.
It looks like billiard, which is a drop in replacement for multiprocessing with some of the patches from bugs.python.org/issue8713 applied, fixes the example above if you use billiard.forking_enable(False). I hope to do a more realistic test for my use case later, but this might be a good workaround for pythons < 3.4 without this feature.
Does the resolution of this issue: zeromq/pyzmq#613 in pyzmq sufficiently solve the issues others have described here? If so, I think this can be closed - the test case in my original comment no longer hangs since upgrading to pyzmq==14.5.0.
Activity
ahaefner commentedon Jul 18, 2014
I'm seeing the same issue when running multiprocessing code from the ipython-qtconsole. I have a similar system to you, but I'm running Anaconda with python 2.7.8
erccarls commentedon Jul 28, 2014
Having the same problem on both EPD and Anaconda w/ python 2.7
andaag commentedon Jul 29, 2014
Check that you're running the same version of the zmq library and the zmq python lib.
ricklupton commentedon Jul 29, 2014
I think the problem here is that multiprocessing on Unix uses fork() by default to start the worker processes. Mixing forking (used by multiprocessing) and threading (used by IPython/ZMQ) is not necessarily supposed to work in the first place (see e.g. this bug). It's possible that the
ZMQErroris due to there being two copies of ZMQ trying to connect to the same port after the fork.If you're using Python 3.4, multiprocessing can now avoid forking by using the "forkserver" or "spawn" start methods. I've checked that your example fails for me (IPython master, Python 3.4, Ubuntu 14.04), but adding either
or
before importing the example in the IPython notebook makes it work fine.
It may be possible to make IPython "fork-safe" -- for example, the original discussion in this bug was about making the
loggingmodule work with fork -- but I don't know what this would involve.jamescasbon commentedon Nov 12, 2014
It looks like billiard, which is a drop in replacement for multiprocessing with some of the patches from bugs.python.org/issue8713 applied, fixes the example above if you use
billiard.forking_enable(False). I hope to do a more realistic test for my use case later, but this might be a good workaround for pythons < 3.4 without this feature.jamescasbon commentedon Nov 12, 2014
Trying out billiard with billiard.Pool.map crashed the kernel :(
ellisonbg commentedon Jan 11, 2015
Targeting this for 4.0 unless someone wants to tackle it soon.
Shotgunosine commentedon Feb 17, 2015
I'm using joblib's Parallel and delayed, and they have been giving me this error intermittently.
It seems like I don't get this error if I put the offending code inside a function and then call that function from a second cell. I hope this helps.
khadiwala commentedon Mar 2, 2015
Does the resolution of this issue: zeromq/pyzmq#613 in pyzmq sufficiently solve the issues others have described here? If so, I think this can be closed - the test case in my original comment no longer hangs since upgrading to pyzmq==14.5.0.
jamescasbon commentedon Mar 2, 2015
Agreed, new pyzmq seems to fix this.
minrk commentedon Mar 2, 2015
Thanks for confirming this is fixed upstream.