zeromq ROUTER连接到ROUTER,丢失了第一条消息

时间:2013-10-10 06:51:10

标签: zeromq router

我的环境: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#代码下没有任何问题,它有什么问题?

python代码:

    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)

0 个答案:

没有答案