当前位置: 动力学知识库 > 问答 > 编程问答 >

python - Why is this implementation of push/pull in zeromq not returning all messages

问题描述:

I have a client server pair in zeromq. What is strange is that the pull client only receives only every other message sent. Here is my implementation

## Push Server

import zmq

def post():

context = zmq.Context()

socket = context.socket(zmq.PUSH)

socket.bind("tcp://127.0.0.1:3333")

socket.send("hello")

socket.close()

if __name__ == "__main__":

post()

## Pull client

def read():

context = zmq.Context()

content = context.socket(zmq.PULL)

content.connect("tcp://127.0.0.1:3333")

while True:

print content.recv()

if content.recv() is "0":

sys.exit()

if __name__ == "__main__":

read()

Why is read() only receiving half of all messages?

网友答案:

You get a new message every time you call content.recv(). That's one for the print statement and another for the if clause. Read the message into a local variable instead. As a side note, use '==', not 'is' for the compare.

while True:
    msg = content.recv()
    print msg
    if msg == "0":
        sys.exit()
分享给朋友:
您可能感兴趣的文章:
随机阅读: