Skip to content

Instantly share code, notes, and snippets.

@bmjames
Created April 17, 2011 23:45
Show Gist options
  • Save bmjames/924612 to your computer and use it in GitHub Desktop.
Save bmjames/924612 to your computer and use it in GitHub Desktop.
Async ZeroMQ example using gevent_zeromq
import gevent
from gevent_zeromq import zmq
context = zmq.Context()
def sub():
subscriber = context.socket(zmq.SUB)
subscriber.connect('tcp://localhost:5561')
subscriber.setsockopt(zmq.SUBSCRIBE, '')
while True:
msg = subscriber.recv()
print "Subscriber received:", msg
def pub():
publisher = context.socket(zmq.PUB)
publisher.bind('tcp://*:5561')
for msg in (str(i) for i in xrange(5)):
print "Publishing message:", msg
publisher.send(msg)
gevent.sleep(1)
gevent.spawn(sub)
gevent.spawn(pub)
gevent.sleep(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment