Design wise and performance wise which approach is recommended for handling multiple Zeromq sockets and why ?
Is it true that Tornado's IOLoop used by ZeroMQ hogs less CPU than the Poller used in a while loop for handling multiple sockets ?
It would be nice if you add your own observation / analysis to your question.
I don't think there is any difference in performance but there is a difference in design.
In case of poller
You register your sockets to be polled and then you use if blocks
to identify and work with each socket.
while should_continue:
socks = dict(poller.poll())
if socket_pull in socks and socks[socket_pull] == zmq.POLLIN:
Work_on_socket_pull ....
if socket_sub in socks and socks[socket_sub] == zmq.POLLIN:
Work_on_socket_sub ....
In case of eventloop
But using event loop is quite elegant when you are handling multiple sockets since you register callbacks
.
stream_pull = zmqstream.ZMQStream(socket_pull)
stream_pull.on_recv(getcommand)
stream_sub = zmqstream.ZMQStream(socket_sub)
stream_sub.on_recv(process_message)
As you can notice from the second example, the if blocks are removed. You write your socket messaging operation else where and you register your callback methods when socket is ready. In this case on_recv()
I hope that clarifies your question.
The Tornado IO Loop, as reimplemented by PyZMQ, uses the poller behind the scenes anyway, so it is unlikely that one would use more CPU than the other.
See https://github.com/zeromq/pyzmq/blob/master/zmq/eventloop/ioloop.py for details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With