I just got started with ZMQ. I am designing an app whose workflow is:
- one of many clients (who have random PULL addresses) PUSH a request to a server at 5555
- the server is forever waiting for client PUSHes. When one comes, a worker process is spawned for that particular request. Yes, worker processes can exist concurrently.
- When that process completes it’s task, it PUSHes the result to the client.
I assume that the PUSH/PULL architecture is suited for this. Please correct me on this.
But how do I handle these scenarios?
- the client_receiver.recv() will wait for an infinite time when server fails to respond.
- the client may send request, but it will fail immediately after, hence a worker process will remain stuck at server_sender.send() forever.
So how do I setup something like a timeout in the PUSH/PULL model?
EDIT: Thanks user938949’s suggestions, I got a working answer and I am sharing it for posterity.
If you are using zeromq >= 3.0, then you can set the RCVTIMEO socket option:
But in general, you can use pollers:
And
poller.poll()takes a timeout:evtswill be an empty list if there is nothing to receive.You can poll with
zmq.POLLOUT, to check if a send will succeed.Or, to handle the case of a peer that might have failed, a:
might suffice, which will always return immediately – raising a ZMQError(zmq.EAGAIN) if the send could not complete.