带有zeromq PUSH / PULL的Eventlet无法按预期工作

时间:2012-05-15 09:41:53

标签: python zeromq eventlet

我正在尝试使用eventlet和zeromq PUSH / PULL套接字创建一个侦听器/发送器对。

这是代码:

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')

我得到了输出:

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

而不是预期的:

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

我错过了什么?

1 个答案:

答案 0 :(得分:1)

您应该使用bind而不是connect作为发送方套接字,因此:

s.connect(adr)

应更改为:

s.bind(adr)