zmq 轮询器如何工作?

编程入门 行业动态 更新时间:2024-10-28 06:35:24
本文介绍了zmq 轮询器如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我对轮询器在 zmq 中实际执行的操作感到困惑.zguide 对其进行了最低限度的介绍,并且仅将其描述为从多个套接字读取的一种方式.这对我来说不是一个令人满意的答案,因为它没有解释如何拥有超时套接字.我知道 zeromq:如何防止无限等待? 解释推/拉,但不是 req/rep 模式,这是我想知道如何使用的.

I am confused as to what poller actually does in zmq. The zguide goes into it minimally, and only describes it as a way to read from multiple sockets. This is not a satisfying answer for me because it does not explain how to have timeout sockets. I know zeromq: how to prevent infinite wait? explains for push/pull, but not req/rep patterns, which is what I want to know how to use.

我想问的是:轮询器是如何工作的,它的功能如何应用于跟踪套接字及其请求?

What I am attempting to ask is: How does poller work, and how does its function apply to keeping track of sockets and their requests?

推荐答案

当你需要在同一个线程中监听不同的套接字时,使用轮询器:

When you need to listen on different sockets in the same thread, use a poller:

ZMQ.Socket subscriber = ctx.socket(ZMQ.SUB)
ZMQ.Socket puller = ctx.socket(ZMQ.PULL)

使用轮询器注册套接字(POLLIN 侦听传入消息)

Register sockets with poller (POLLIN listens for incoming messages)

ZMQ.Poller poller = ZMQ.Poller(2)
poller.register(subscriber, ZMQ.Poller.POLLIN)
poller.register(puller, ZMQ.Poller.POLLIN)

轮询时,使用循环:

while( notInterrupted()){
  poller.poll()

  //subscriber registered at index '0'
  if( poller.pollin(0)) 
     subscriber.recv(ZMQ.DONTWAIT)

  //puller registered at index '1'
  if( poller.pollin(1))
     puller.recv( ZMQ.DONTWAIT)
}

选择您想要的投票方式...

Choose how you want to poll...

poller.poll() 阻塞,直到任一套接字上有数据.
poller.poll(1000) 阻塞 1 秒,然后超时.

poller.poll() blocks until there's data on either socket.
poller.poll(1000) blocks for 1s, then times out.

轮询器会在套接字上有可用数据(消息)时发出通知;阅读它是你的工作.

The poller notifies when there's data (messages) available on the sockets; it's your job to read it.

读取时,不要阻塞:socket.recv(ZMQ.DONTWAIT).即使 poller.pollin(0) 检查是否有要读取的数据,您也希望避免在轮询循环内进行任何阻塞调用,否则,您最终可能会由于卡住"套接字而阻塞轮询器.

When reading, do it without blocking: socket.recv( ZMQ.DONTWAIT). Even though poller.pollin(0) checks if there's data to be read, you want to avoid any blocking calls inside the polling loop, otherwise, you could end up blocking the poller due to 'stuck' socket.

因此,如果向 subscriber 发送两个单独的消息,则必须调用 subscriber.recv() 两次以清除轮询器,否则,如果调用subscriber.recv() 一次,轮询器会不断告诉您还有另一条消息要阅读.因此,从本质上讲,轮询器跟踪的是消息的可用性和数量,而不是实际的消息.

So, if two separate messages are sent to subscriber, you have to invoke subscriber.recv() twice in order to clear the poller, otherwise, if you call subscriber.recv() once, the poller will keep telling you there's another message to be read. So, in essence, the poller tracks the availability and number of messages, not the actual messages.

您应该浏览轮询示例并使用代码,这是最好的学习方式.

You should run through the polling examples and play with the code, it's the best way to learn.

这是否回答了您的问题?

Does that answer your question?

这篇关于zmq 轮询器如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-23 13:14:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1042352.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:工作   zmq   轮询器

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!