How can I prevent pyzmq from blocking my python application?

Viewed 20

This is my code:

    def _poll_for_messages(self, poller: Poller):
    sockets = dict(poller.poll(3000))
    if not sockets:
        self._reconnect_if_necessary(poller)
        return
    if self._command_handler.command_socket in sockets:
        encoded_message = self._command_handler.command_socket.recv_multipart()

This should communicate with my service bus and potentially reconnect if the bus gets restarted. When the Bus gets shut down, sometimes the last line still gets reached but the socket is not able to receive a message and it waits for one indefinitely.

For normal receives there is zmq.DONTWAIT but this does not work for multipart messages as far as I'm aware. Is there an easy way around this or am I polling for messages the wrong way in general?

1 Answers

If anyone stumbles over this and has the same problem, mine got fixed by adding the zmq.POLLIN flag when registering a socket to my poller:

    poller.register(self._command_handler._command_socket, zmq.POLLIN)
Related