Skip to content Skip to sidebar Skip to footer

Multithreading And Zmq Dealer/rep Hello World Doesn't Work

First of all my code (largely inspired from ZMQ doc http://zguide.zeromq.org/py:mtserver): import zmq import time import sys import threading #SOCKET_NAME = 'tcp://127.0.0.1:8000'

Solution 1:

I made it work by:

  • for the dealer, sending your message in multipart, the first part being an empty message, the second part being your message
  • reduced the timer (that one didn't help though)

Here is the code:

import zmq
import time
import sys
import threading

SOCKET_NAME = "tcp://127.0.0.1:8000"#SOCKET_NAME = "inproc://mysocket"defdealerRoutine(context):
    socket = context.socket(zmq.DEALER)
    socket.bind(SOCKET_NAME)
    time.sleep(1)
    socket.send("", zmq.SNDMORE)
    socket.send("hello")
    socket.send("", zmq.SNDMORE)
    socket.send("hello")
    print socket.recv()
    print socket.recv()
    socket.close()


defworkerRoutine(context):
    socket = context.socket(zmq.REP)
    socket.connect(SOCKET_NAME)
    s = socket.recv()
    print s
    socket.send("world")

context = zmq.Context()

workers = []
for i inrange(0, 2):
    worker = threading.Thread(target=workerRoutine, args=([context]))
    workers.append(worker) 
    worker.start()

dealerRoutine(context)

for worker in workers:
    worker.terminated = True

context.term()

Post a Comment for "Multithreading And Zmq Dealer/rep Hello World Doesn't Work"