nam
nam

Reputation: 3632

Using process instead of thread with zeromq

I'm reading this code http://zguide.zeromq.org/py:mtserver But when I've tried to replace threading.Thread by multiprocessing.Process I got the error

Assertion failed: ok (mailbox.cpp:84)

Code is

import time
import threading
import zmq

def worker_routine(worker_url, context=None):
    """Worker routine"""
    context = context or zmq.Context.instance()
    # Socket to talk to dispatcher
    socket = context.socket(zmq.REP)

    socket.connect(worker_url)

    while True:

        string  = socket.recv()

        print("Received request: [ %s ]" % (string))

        # do some 'work'
        time.sleep(1)

        #send reply back to client
        socket.send(b"World")

def main():
    """Server routine"""

    url_worker = "inproc://workers"
    url_client = "tcp://*:5555"

    # Prepare our context and sockets
    context = zmq.Context.instance()

    # Socket to talk to clients
    clients = context.socket(zmq.ROUTER)
    clients.bind(url_client)

    # Socket to talk to workers
    workers = context.socket(zmq.DEALER)
    workers.bind(url_worker)

    # Launch pool of worker threads
    for i in range(5):
        process = multiprocessing.Process(target=worker_routine, args=(url_worker,))
        process.start()

    zmq.device(zmq.QUEUE, clients, workers)

    # We never get here but clean up anyhow
    clients.close()
    workers.close()
    context.term()

if __name__ == "__main__":
    main()

Upvotes: 1

Views: 1405

Answers (1)

zeFrenchy
zeFrenchy

Reputation: 6591

The limitations of each transport is detailed in the API.

inproc is for intra-process communication (i.e. threads). You should try ipc which support inter-process communication or even just tcp.

Upvotes: 2

Related Questions