0

我正在尝试使用 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
...

我错过了什么?

4

1 回答 1

1

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

s.connect(adr)

应改为:

s.bind(adr)
于 2012-05-15T10:35:14.217 回答