在设计和性能方面推荐哪种方法来处理多个 Zeromq 套接字,为什么?
ZeroMQ 使用的 Tornado 的 IOLoop 占用的 CPU 比 while 循环中用于处理多个套接字的 Poller 占用的 CPU 少,这是真的吗?
最佳答案
如果您将自己的观察/分析添加到您的问题中,那就太好了。
我认为在性能上没有任何差异,但在设计上存在差异。
在轮询的情况下
您注册要轮询的套接字,然后使用 if blocks
来识别和使用每个套接字。
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 ....
在事件循环的情况下
但是当你处理多个套接字时使用事件循环是非常优雅的,因为你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)
您可以从第二个示例中注意到,if block 已被删除。您在其他地方编写套接字消息传递操作,并在套接字准备就绪时注册回调方法。 在这种情况下 on_recv()
我希望这能澄清您的问题。
关于python - ZeroMQ 轮询器 vs Tornados EventLoop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11192489/