[zeromq-dev] Fundamentals of a REQ/REP

Chuck Remes cremes.devlist at mac.com
Fri Sep 30 23:03:08 CEST 2011

On Sep 30, 2011, at 3:53 PM, Daniel Hyams wrote:

> What I'm about to ask is so basic (very green to sockets and ZMQ), I have to be misunderstanding something fundamental :(  But I have been through the guide and searched the archives, and have not come up with a good answer for this.
> All I'm trying to do is have a server and client talk back and forth with a REQ/REP pattern.  (Client REQ, Server REP).  As long as the server side is up, everything is fine.
> But, what happens on the client end is unsavory, if the server is down.  What I would like to happen is for the client to be able to tell the user "hey, the server is down, and your request isn't going to happen" if the server does not respond to a REQ.  I cannot seem to make this happen....
> If the client does (in Python):
>   sock.send("the reqeust")
>   reply = sock.recv()
> then this deadlocks when the servers is down, obviously.  
> If the client does:
>   sock.send("the request")
>   poller = zmq.Poller();
>   poller.register(sock.zmq.POLLIN)
>   socks = dict(poller.poll(1000))
>   if sock in socks:
>       reply = sock.recv()
>   else:
>       print "The server is not alive"
>       # oops, now the lockstep send/recv required for a REQ/REP setup is broken!  
>       # socket is in the wrong state for a send.
> Then I get into trouble also, because the socket gets in the wrong state...I want to send next, not receive.  But I don't see any way of forcing the socket back into a "sending" state.
> I also tried a PAIR instead of REQ/REP, and did much better with that one, but since ZMQ queues messages on the sending end, my server gets all of the (undesired) queued up messages upon startup, and the client is not ready to receive the resulting replies.
> So I'm mystified.  Is the answer a ROUTER/DEALER setup?

The answer is to close the socket in the client and reopen it. This will "reset" its state. Use that in conjunction with the polling method you wrote about and it will be fine.


More information about the zeromq-dev mailing list