-
-
Save jkarneges/ab2b1abea1ee4cfc1332 to your computer and use it in GitHub Desktop.
router not routing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ cat ztest1.py | |
import time | |
import zmq | |
ctx = zmq.Context() | |
req_sock = ctx.socket(zmq.REQ) | |
req_sock.connect('ipc://req-test') | |
router_sock = ctx.socket(zmq.ROUTER) | |
router_sock.setsockopt(zmq.ROUTER_MANDATORY, 1) | |
router_sock.connect('ipc://router-test') | |
while True: | |
print 'querying for id' | |
req_sock.send('get') | |
id = req_sock.recv() | |
print 'id is %s. sending message via router' % id | |
try: | |
router_sock.send_multipart([id, '', 'hello']) | |
except zmq.error.ZMQError as e: | |
print 'failed to send via router: %s' % e | |
time.sleep(2) | |
$ cat ztest2.py | |
import time | |
import uuid | |
import zmq | |
ctx = zmq.Context() | |
instance_id = str(uuid.uuid4()) | |
rep_sock = ctx.socket(zmq.REP) | |
rep_sock.bind('ipc://req-test') | |
router_sock = ctx.socket(zmq.ROUTER) | |
router_sock.identity = instance_id | |
router_sock.bind('ipc://router-test') | |
poller = zmq.Poller() | |
poller.register(rep_sock, zmq.POLLIN) | |
poller.register(router_sock, zmq.POLLIN) | |
while True: | |
socks = dict(poller.poll()) | |
if socks.get(rep_sock) == zmq.POLLIN: | |
rep_sock.recv() | |
print 'responding to query for id' | |
rep_sock.send(instance_id) | |
elif socks.get(router_sock) == zmq.POLLIN: | |
print router_sock.recv_multipart() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment