ZeroMQ PUB/SUB memo

def sub_handler(sock, events):
    msg = sock.recv()
    print msg

def sub():
    print "*** sub"
    loop = ioloop.IOLoop.instance()
    ctx = zmq.Context()
    sock = ctx.socket(zmq.SUB)
    sock.connect('tcp://127.0.0.1:5555')
    sock.setsockopt(zmq.SUBSCRIBE, '')
    loop.add_handler(sock, sub_handler, zmq.POLLIN)
    loop.start()

def pub():
    print "*** pub"
    ctx = zmq.Context()
    sock = ctx.socket(zmq.PUB)
    sock.bind('tcp://127.0.0.1:5555')
    for i in range(0,10):
        msg = "[%d] Hello" % i
        print "publishing %s" % msg
        sock.send(msg)
        time.sleep(0.5)

Leave a Reply

Your email address will not be published. Required fields are marked *