[zeromq-dev] Anyone managed to use zeromq with Qt (QSocketNotifier) ?

2 views
Skip to first unread message

Gaspard Bucher

unread,
Sep 19, 2011, 2:41:55 PM9/19/11
to ØMQ development list
Hi list !

I am trying to use zeromq with a Qt application and I want to avoid having multiple threads
so I tried to use ZMQ_FD and pass this to QSocketNotifier but only the first message is
signaled. My guess is that zmq does not keep the file descriptor in read-ready state but
just signals the first element ?

Pseudo-code:

1. get zmq fd
2. create QSocketNotifier with 'Read' events
3. app exec
..
4. first (or many) messages arrive
5. ---> QSocketNotifier fires
6. get 1 message
7. wait for QSocketNotifier to fire again
8. .... never happens

My guess is that there is something I do not understand regarding the ZMQ_FD file descriptor.
Maybe it only retriggers "read-ready" if the queue has been emptied ?

Thanks for your help,  

                                                               Gaspard

Mikko Koppanen

unread,
Sep 19, 2011, 2:45:54 PM9/19/11
to gas...@teti.ch, ZeroMQ development list
On Mon, Sep 19, 2011 at 7:41 PM, Gaspard Bucher <gas...@teti.ch> wrote:
> My guess is that there is something I do not understand regarding the ZMQ_FD
> file descriptor.
> Maybe it only retriggers "read-ready" if the queue has been emptied ?

Hi,

the ZMQ_FD is edge-triggered rather than level triggered, which means
that if you have multiple messages pending the readable state will be
triggered once, rather than continuously. If you read all messages in
your callback the ZMQ_FD should trigger readable event again on new
messages.

--
Mikko Koppanen
_______________________________________________
zeromq-dev mailing list
zerom...@lists.zeromq.org
http://lists.zeromq.org/mailman/listinfo/zeromq-dev

Chuck Remes

unread,
Sep 19, 2011, 2:46:00 PM9/19/11
to ZeroMQ development list

On Sep 19, 2011, at 1:41 PM, Gaspard Bucher wrote:

> Hi list !
>
> I am trying to use zeromq with a Qt application and I want to avoid having multiple threads
> so I tried to use ZMQ_FD and pass this to QSocketNotifier but only the first message is
> signaled. My guess is that zmq does not keep the file descriptor in read-ready state but
> just signals the first element ?
>
> Pseudo-code:
>
> 1. get zmq fd
> 2. create QSocketNotifier with 'Read' events
> 3. app exec
> ..
> 4. first (or many) messages arrive
> 5. ---> QSocketNotifier fires
> 6. get 1 message
> 7. wait for QSocketNotifier to fire again
> 8. .... never happens
>
> My guess is that there is something I do not understand regarding the ZMQ_FD file descriptor.
> Maybe it only retriggers "read-ready" if the queue has been emptied ?

Your last statement is correct. You need to read all messages from the queue before it can trigger again.

cr

Maciej Gajewski

unread,
Sep 20, 2011, 2:59:06 AM9/20/11
to gas...@teti.ch, ZeroMQ development list
I did it and it works nicely on Linux and Windows.

You need to disable the notifier before calling ZMQ_EVENTS (and
reading the message if available) and enable if afterwards.

Pseudocode:

void OnSocketReadable(zmqsocket, notifier)
{
notifier.setEnabled(false);
CheckForEventsAndRead(zmqsocket);
notifier.setEnabled(true);
}

bool HasIncoming(zmqsocket)
{
qint32 events = 0;
std::size_t eventsSize = sizeof(events);
zmqsocket.getsockopt(ZMQ_EVENTS, &events, &eventsSize);
return (events & ZMQ_POLLIN);
}

void CheckForEventsAndRead(zmqsocket)
{
if (HasIncoming(zmqsocket))
ReadMessage(zmqsocket);
}

cheers,

Maciek

Gaspard Bucher

unread,
Sep 20, 2011, 2:02:32 PM9/20/11
to Maciej Gajewski, ZeroMQ development list
Hi everyone !

Thanks for your support: I managed to create a scheduler that automagically switches from zmq_poll to Qt's event loop. I have no idea how I could have coded this in C/C++, but in Lua it works seamlessly thanks to coroutines. Pseudo-code:

1. check for event (getsockopt...)
2. if event not ready
2.1 register filedescriptor in QSocketNotifier
2.2 ... yield
3. event is ready: recv

Real code (also makes sure the gui thread cannot be choked by an inbox that is never emptied):


When Qt is not loaded, I use zmq_poll for all sockets (posix, zmq) and timers and it's all working like a charm. Choosing zmq for my network layer was definitely very well inspired.

Thank you guys !
                                                               Gaspard

Peter Alexander

unread,
Sep 21, 2011, 12:53:31 AM9/21/11
to gas...@teti.ch, ZeroMQ development list, Maciej Gajewski

There the two Qt ports on the project labs page[1]. I think the nzmqt
one is more complete.. not sure.

[1] http://www.zeromq.org/docs:labs#toc6

Reply all
Reply to author
Forward
0 new messages