[zeromq-dev] pyzmq and multiprocessing
Jon Dyte
jon at totient.co.uk
Thu Dec 22 19:13:37 CET 2011
On 22/12/11 15:03, Ivo Danihelka wrote:
> On Thu, Dec 22, 2011 at 2:48 PM, Wiliam Souza<wiliam at mandriva.com.br> wrote:
>> [1] http://pastebin.mandriva.com/38061
> I don't get the assert on zmq 2.1.4.
> The ControllerServer process does not receive any msg for me.
> I guess, multiprocessing is not compatible with zmq threads.
> The example worked, when I moved the zmq initialization inside run().
>
> I don't know multiprocessing internals. I would avoid mixing it with zeromq.
>
That's correct. It's not going to work creating the zmq Context in the
__init__ method of the Process object.
On unix Python Multiprocess works by forking the script for each Process
object and communicating over some IPC.
The main script coordinates all the other 'Process' objects.
The problem in the example is that the 0MQ context's are created before
the fork, in the __init__ method.
These contexts create zmq threads in the background.
Then when the 'start' methods are executed on each controller, the
forking happens. Threads don't generally survive fork
and thus the process is in an undefined state.
If you put a massive sleep statement ie (time.sleep(86400)) at the
beginning of each start method, you
can clearly see using ps you will see something like
jon 24938 18475 0 17:50 pts/1 00:00:00 python /home/jon/mp.py
jon 24943 24938 0 17:50 pts/1 00:00:00 python /home/jon/mp.py
jon 24944 24938 0 17:51 pts/1 00:00:00 python /home/jon/mp.py
24938 is the actually executed script
24943 and 24944 are the two children executing a Process object each.
However if you look under the /proc/<pid>/tasks
you will see
24938 has 5 threads
ls /proc/24938/task
24938 24939 24940 24941 24942
These are the 'main' thread and two threads per 0MQ context
the child processes both have one thread and hence 0MQ isn't going to
work as they have no zmq threads!
ls /proc/24943/task
24943
ls /proc/24944/task
24944
If you want the script as written to have a chance of working, the 0MQ
context and sockets need to be created in the run method,
or at least after the fork.
Jon
More information about the zeromq-dev
mailing list