Skip to content

Instantly share code, notes, and snippets.

@abargnesi
Last active October 7, 2015 17:28
Show Gist options
  • Select an option

  • Save abargnesi/f7e028295a3df3ef6888 to your computer and use it in GitHub Desktop.

Select an option

Save abargnesi/f7e028295a3df3ef6888 to your computer and use it in GitHub Desktop.
Greenline Test
[[rail]]
name = "broadcast"
pattern = "pub/sub"
ingress = 5000
egress = 6000
[[rail]]
name = "conversation"
pattern = "req/rep"
ingress = 7000
egress = 8000
#!/usr/bin/env python3
from zmq import Context, PUB
from zmq.log.handlers import PUBHandler
import time
# setup
ctx = Context()
pub_sock = ctx.socket(PUB)
# publisg msg to channel
pub_sock.connect('tcp://localhost:5000')
pub_sock.send_multipart([b'', b'Hello'])
time.sleep(1)
#!/usr/bin/env python3
from zmq import Context, SUB, SUBSCRIBE
ctx = Context()
s = ctx.socket(SUB)
s.connect('tcp://localhost:6000')
s.setsockopt(SUBSCRIBE, b'')
topic, msg = s.recv_multipart()
print("on topic: %s, %s" % (topic, msg))
@abargnesi
Copy link
Copy Markdown
Author

The publisher's send does not appear in the greenline standard out log nor does the subscriber receive a message. However, if I am not running greenline and allow publish.py to bind then it works.

@nbargnesi
Copy link
Copy Markdown

Blocking and non-blocking variants of subscriber:

#!/usr/bin/env python
import os
import zmq
from contextlib import suppress
ctxt = zmq.Context()

s = ctxt.socket(zmq.SUB)
s.connect('tcp://localhost:6000')
s.setsockopt(zmq.SUBSCRIBE, b'') 

print(os.getpid())

def blocking_recv():
    topic, msg = s.recv_multipart()
    return topic, msg

def nonblocking_recv():
    iters = 0
    while True:
        iters += 1 
        with suppress(zmq.error.Again):
            topic, msg = s.recv_multipart(zmq.NOBLOCK)
            break
    print('%d iterations before receipt' % (iters))
    return topic, msg

while True:
    print(nonblocking_recv())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment