如何将 ZeroMQ 套接字集成到 glib 主循环中?
我想将 ZeroMQ 套接字添加到 glib 程序中。
遗憾的是,zmq 套接字不支持 poll(),并且他们给出了自己的实现,这会重载旧的 poll() 函数。我怎样才能有效地将其集成到主循环中?我尝试使用他们的轮询( zmq_poll()
)而不是默认的,但除了将其设为 global
之外,没有好的方法为其提供 zmq 套接字。
定义一个新的GSource
是可行的,但它可能会导致高CPU使用率(通过设置timeout = 0
)或任意轮询超时(例如设置timeout = 100
) > 至少每 100 毫秒轮询一次),这并不是真正高效,因为存在轮询的可能性。
I wanted to add a ZeroMQ socket to a glib program.
The pitty is, a zmq socket is not poll()
-able, and they give their implementation, which overloads the old poll()
func. How could I integrate that into the main loop efficiently? I tried using their poll ( zmq_poll()
) instead of the default one, but there's no good way of giving it the zmq socket, besides from making it a global
.
Defining a new GSource
works, but it can get high CPU usage ( by setting timeout = 0
) or arbitrary poll timeouts ( e.g. setting timeout = 100
to be polled at least every 100 ms ), which is not really efficient, since there is the possibility of polling.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据常见问题解答,您可以尝试“其他方式”方法。不要在 zmq 套接字上进行
poll
,而是尝试在常规套接字上使用zmq_poll()
。查看问题如何将ØMQ套接字与普通套接字集成?或者使用 GUI 事件循环? 在常见问题解答中。
According to FAQ you could try "the other way" approach. Rather than making
poll
on a zmq socket tryzmq_poll()
on a regular socket.Check out question How can I integrate ØMQ sockets with normal sockets? Or with a GUI event loop? in the FAQ.
我发现较新的 zmq 库支持
ZMQ_FD
getsockopt()
参数,它会返回一个 unixfd
,您可以
poll()
。唯一需要注意的是,您不能仅poll()
它来知道是否可以recv()
或send()
from / to它,但您需要使用ZMQ_EVENTS
getsockopt()
参数来获取真实的fd
状态。它似乎在 glib 中工作得很好。
I found that newer zmq libraries support the
ZMQ_FD
getsockopt()
parameter, which gives you back a unixfd
which you canpoll()
. The only caveat is that you can't justpoll()
it to know if you canrecv()
orsend()
from / to it, but you need to use theZMQ_EVENTS
getsockopt()
parameter to get back the realfd
status.It seems to be working quite well in glib.