How to integrate a ZeroMQ socket into a glib main loop?
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
.
Defin开发者_如何转开发ing 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.
According to FAQ you could try "the other way" approach. Rather than making poll
on a zmq socket try zmq_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.
I found that newer zmq libraries support the ZMQ_FD
getsockopt()
parameter, which gives you back a unix fd
which you can poll()
. The only caveat is that you can't just poll()
it to know if you can recv()
or send()
from / to it, but you need to use the ZMQ_EVENTS
getsockopt()
parameter to get back the real fd
status.
It seems to be working quite well in glib.
精彩评论