[zeromq-dev] push/pull and multiple workers

andrea crotti andrea.crotti.0 at gmail.com
Wed Aug 15 12:50:41 CEST 2012


2012/8/15 andrea crotti <andrea.crotti.0 at gmail.com>:
> I'm having a few problems with PUSH/PULL and multiple processes, so I
> tried to make a simple example to understand what's going on, but
> still is not clear..
>
> In the following example I would expect to receive two tasks from the
> two different processes and then quit, instead it receives the first
> one and then hangs..  Why is that? (Also using ipc instead of tcp
> produces the same behaviour).
>
> Are there issues with multiprocessing for example that I should be
> aware of?
>
> import zmq
> from time import sleep
> from multiprocessing import Process
>
> # TODO: this only works on Linux
> PORT = 'tcp://127.0.0.1:4444'
>
>
> def start_send():
>     ctx = zmq.Context()
>     task_sender = ctx.socket(zmq.PUSH)
>     task_sender.bind(PORT)
>     task_sender.send('hello')
>     task_sender.send('world')
>
>
> def start_receive():
>     ctx = zmq.Context()
>     task_recv = ctx.socket(zmq.PULL)
>     task_recv.connect(PORT)
>     msg = task_recv.recv()
>     print(msg)
>
>
> if __name__ == '__main__':
>     p1 = Process(target=start_send)
>     p2 = Process(target=start_receive)
>     p3 = Process(target=start_receive)
>     p1.start()
>     p2.start()
>     p3.start()


I just discovered that adding a sleep between the two send makes it
actually work, so in the "normal" case one message just gets lost, but
why??

    task_sender.send('hello')
    sleep(1)
    task_sender.send('world')



More information about the zeromq-dev mailing list