Abruzzo Forte e Gentile
Abruzzo Forte e Gentile

Reputation: 14869

About ZeroMQ and polling on non-blocking sockets :

In Linux, if an application uses raw non-blocking sockets for reading, in conjunction with poll, will it be possible to be notified when a socket has some data available for reading or not?

I tried to dig into ZeroMQ documentation, but I cannot find my way.

I found the flag ZMQ_NOBLOCK, but it is applied when called zmq_recv.

Is there a way to set the socket itself as Non-Blocking so that poll can return immediately if there is nothing to read?

I am using C++ as language for my app.

Upvotes: 1

Views: 3245

Answers (1)

user3666197
user3666197

Reputation: 1

Well, there are few missed points here:

First, a ZeroMQ-Socket-instance is not anything like an O/S socket. For principal disambiguation, feel free to read posts on conceptual differences.

This is an often re-articulated misconception, so let me repeat it in bold.

Beware: ZeroMQ Socket()-instance is not a tcp-socket-as-you-know-it. Best read about the main conceptual differences in [ ZeroMQ hierarchy in less than a five seconds ] or other posts and discussions here


The ZMQ_NOBLOCK symbol can be used in more places:

the zmq_recv() being one such, zmq_send() being another.

This symbol is principaly #define-ed in header file, but somewhere having also some other value-identical aliases, a zmq.DONTWAIT being one such flag-composition related symbol for python-wrappers/bindings.


The Socket-instance has no such property as (not-)being blocking:

this is the core principal miss in your so far posted questions.

The Socket-instance does not persist any such feature per-se.

The respective ( individual ) instance operation - a call to a { .recv() | .send() }-method can use a parameter-based mechanism to change it's per-call modus operandi:
flags = { 0, ZMQ_DONTWAIT, ZMQ_SENDMORE, ZMQ_DONTWAIT && ZMQ_SENDMORE } but having no such option to "turn"-any-such mode on/off to persist.


Q: "Is there a way to set the socket itself as Non-Blocking"?
A: No.


Q: "Is there a way to set the Poller.poll() to return immediately"?
A: YES!

int zmq_poll ( zmq_pollitem_t *items, int nitems, long timeout );

so,
unless one calls zmq_poll( ..., -1 ), which turns the polling hang indefinitely long, the zmq_poll()-call returns as late as timeout [ms], so using timeout == 0, the zmq_poll()-call returns immediately, whether there has been found any item or not, that's great, isn't it?

Upvotes: 4

Related Questions