Question

I am trying to create a listener/sender pair using eventlet and zeromq PUSH/PULL sockets.

This is the code:

import eventlet
from eventlet.green import zmq
from eventlet import sleep as gsleep

#===============================================================================
# config
#===============================================================================
ctx = zmq.Context()
adr = 'tcp://127.0.0.1:5558'

#===============================================================================
# listener
#===============================================================================
def listen(addr):
    l = ctx.socket(zmq.PULL)
    l.connect(addr)
    print 'listening on %s' % addr
    while True:
        data = l.recv()
        print 'something received : %s' % data

eventlet.spawn_n(listen, adr)

#===============================================================================
# sender
#===============================================================================
s = ctx.socket(zmq.PUSH)
s.connect(adr)
while True:
    gsleep(1)
    print "sending something"
    s.send('blabla')

I'm getting the output:

listening on tcp://127.0.0.1:5558
sending something
sending something
sending something
sending something
sending something
sending something
...

instead of the expected:

listening on tcp://127.0.0.1:5558
sending something
something received : blabla
sending something
something received : blabla
sending something
something received : blabla
...

What am I missing?

Was it helpful?

Solution

You should use bind instead of connect for the sender socket, thus:

s.connect(adr)

should be changed to:

s.bind(adr)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top