我的环境:python 2.7、pyzmq-13.1.0-py2.7-win32、windows 7 sp1 x64
- 2 个 ZMQ_ROUTER 套接字,A 和 B;
- A 绑定到一个地址并被分配一个身份;
- B被分配一个身份并连接到A;
- B 向 A 发送消息,那么第一条消息总是丢失
但是在C#代码下没有任何问题,它有什么问题?
蟒蛇代码:
import time
import struct
import zmq
import threading
def relay(contextBind):
backend = contextBind.socket(zmq.ROUTER)
identity = '2'
backend.setsockopt(zmq.IDENTITY, identity )
backend.bind('tcp://*:5678')
poller = zmq.Poller()
poller.register(backend, zmq.POLLIN)
while True:
try:
sockets = dict(poller.poll())
except Exception,ex:
print ex
break
if backend in sockets:
if sockets[backend] == zmq.POLLIN:
msg = backend.recv_multipart()
print msg
if __name__ == '__main__':
#the first message is lost
context = zmq.Context()
relayThread = threading.Thread(target=relay, args=(context,))
relayThread.start()
frontend = context.socket(zmq.ROUTER)
identity = '3'
frontend.setsockopt(zmq.IDENTITY, identity )
try:
frontend.connect('tcp://127.0.0.1:5678')
except zmq.error.ZMQError, ex:
print ex
i = 1
while True:
print 'send ', str(i),'\n'
frontend.send('2', zmq.SNDMORE)
frontend.send(str(i) + ' msg')
i = i + 1
time.sleep(1)