0

Right now I have the following structure in my code. The request is directly parsed as JSON.

while True:
   if self.rest_sock.poll(timeout=0):
      request = self.rest_sock.recv_json()
      ...

I want to replace the loop with an asynchronous call of a function (to reduce CPU time as explained here: https://stackoverflow.com/a/21937311/10555800). This is done by registering a function as an event handler by using on_recv(). But the JSON message is not parsed. I assume I could parse it myself as explained e.g. here https://stackoverflow.com/a/34242555/10555800. But I was wondering why there nothing equivalent to socket.recv_json() for an async receive of json messages like on_recv_json().

Edit (Answering Questions from @bazza):

  • There is something else for my program to do in the meantime. This did lead to some other issues and I have already fixed this by setting the timeout to None.
  • I assume I could also remove the poll as you suggested as everything related to the socket connection is running in its own thread and therefore should not block the complete program
Tobias
  • 319
  • 1
  • 3
  • 18
  • 1
    So, I think we're back to the code snippet you've already got being about right. The poll with a timeout of zero returns immediately if there's no ready message, and the script goes on to do that something else. Provided that that's not too long and inherently loopy in nature (the `while` loop), you'll be coming back to the poll fairly soon. If that indicates a message is now ready, then the only thing that might happen in this thread is the JSON parsing, because ZMQ's own threads will have already handled the reading of the message across the network. – bazza Jul 17 '21 at 08:35
  • 1
    Alternatively, get rid of the poll altogether, and call the socket receive passing the approriate flag for non-blocking. – bazza Jul 17 '21 at 08:38

1 Answers1

1

What else is going on in the code?

  • You only poll with a timeout of 0 if there's something else for the program to do when there are no inbound messages available. When the poll returns indicating no socket is ready, have the program go do that something else
  • You poll several sockets (see here) with a non-zero timeout if you need to react to whichever of several sockets becomes ready to read, and have nothing to do in the meantime.
  • If you have only one socket, and you're simply waiting for a message to arrive before doing anything else, simply call recv(), which will block until a message turns up. Polling in that case is not necessary.

It's helpful to know a dictionary definition of "poll" as meaning "check on the status of".

bazza
  • 7,580
  • 15
  • 22
  • My problem can be fixed with this suggestion, but it does not answer the question whether there is something like a `on_recv` method for JSON. – Tobias Jul 16 '21 at 06:46
  • 1
    @Tobias, A `on_recv_json()` function doesn't make any sense, I think. `on_recv()` registers a call back but doesn't itself read the socket. As to what the call back does, that's nothing to do with `on_recv()`, and so messages being JSON encoded is irrelevant. The call back would call `socket.recv_json()`. Also, messages being JSON is also irrelevant to ZMQ - `recv_json()` is going to simply be a convenience function that uses `recv()` to receive a message, and then call a JSON parser on the message. – bazza Jul 17 '21 at 08:14
  • 1
    @Tobias, also, what I'm suggesting in the answer is that you likely don't need on_recv() at all. It all depends on whether you want your application to be `Pro-actor` or `Reactor`. If you're intent on using zmq polling, that's `Reactor`, and you don't register callbacks, you simply call socket.recv() when the poller says a message has arrived. If you're intent on using on_recv(), that's `Proactor`, and you'd not also use a zmq poller. Mixing `Proactor` and `Reactor` together in one application is asking for a lot of difficulties! – bazza Jul 17 '21 at 08:19
  • 1
    Alright thank you very much! I think you are correct and it is not necessary to use `on_recv` in my case. – Tobias Jul 19 '21 at 08:21