我正在尝试使用 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
...
我错过了什么?