Question

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?

Was it helpful?

Solution

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()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top