Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ZeroMQ REQ/REP server error handling

Tags:

zeromq

pyzmq

I am trying to use the ZeroMQ rep/req and cannot figure out how to handle server side errors. Look at the code from here:

socket.bind("tcp://*:%s" % port)
while True:
    #  Wait for next request from client
    message = socket.recv()
    print "Received request: ", message
    time.sleep (1)  
    socket.send("World from %s" % port)

My problem is what happens if the client calls socket.send() and then hangs or crashes. Wouldn't the server just get stuck on socket.send() or socket.recv() forever?

Note that it is not a problem with TCP sockets. With TCP sockets I can simply break the connection. With ZMQ, the connections are implicitly managed for me and I don't know if it is possible to break a 'session' or 'connection' and start over.

like image 793
Tom Bennett Avatar asked Feb 07 '14 21:02

Tom Bennett


People also ask

Is ZeroMQ asynchronous?

ZeroMQ is an asynchronous network messaging library known for its high performance. It's intended use is for distributed systems as well as concurrent systems. In summary, ZMQ allows you to send messages (binary data, serialized data, simple strings, etc.)

What is Rep socket?

A REP socket is used by a service to receive requests from and send replies to a client. This socket type allows only an alternating sequence of receive and subsequent send calls. Each request received is fair-queued from among all clients, and each reply sent is routed to the client that issued the last request.

What is ZMQ context?

ZMQ. Context Context is an object serving as a container for all the sockets of a single process. By creating a new context, you start one or more input/output threads: DEFINE context ZMQ.Context. Associated methods: socket()

What is ZMQ Python?

ZeroMQ (also spelled ØMQ, 0MQ or ZMQ) is a high-performance asynchronous messaging library, aimed at use in distributed or concurrent applications. It provides a message queue, but unlike message-oriented middleware, a ZeroMQ system can run without a dedicated message broker.


2 Answers

You can terminate ZMQ sockets much the same way you terminate TCP sockets.

socket.close()

If you need to wait on a message but only up for a finite amount of time you can pass a timeout flag to socket.recv(timeout=1024) and then handle the timeout error case the same way you would when a TCP socket timeouts or disconnects. If you need to manage several sockets all of which may be in an error state then the Poller class will let you accomplish this.

like image 62
Stephen Diehl Avatar answered Sep 21 '22 07:09

Stephen Diehl


The ZMQ Z-guide offers lots of good hints on how to structure your services to handle different scenarios.

I think chapter 4 can be of interest to you, especially the Lazy Pirate Pattern.

Check out the examples of Lazy Pirate Server and Lazy Pirate Client.

like image 28
StianE Avatar answered Sep 19 '22 07:09

StianE