[zeromq-dev] a question about ZMQ_POlL and usage pattern

dev at innercircleproject.com dev at innercircleproject.com
Sat Dec 15 22:08:55 CET 2012


I have some questions about  ZMQ_POL : I am not sure I understand fully 
the usage pattern of ZMQ_POLL :)

For me zmq_poll is about blocking on a number of sockets waiting for a 
set of specifics events. (from http://api.zeromq.org/2-1:zmq-poll)

But I often see the following usage pattern (in fact it is all over the 
ZMQ guide) and I start to wonder about the semantic :

(in python sorry curly brace lovers ;) )

(from http://zguide.zeromq.org/py:mspoller)

# Initialize poll set
poller = zmq.Poller()
poller.register(receiver, zmq.POLLIN)
poller.register(subscriber, zmq.POLLIN)

# Process messages from both sockets
while True:
     socks = dict(poller.poll())

     if receiver in socks and socks[receiver] == zmq.POLLIN:
         message = receiver.recv()
         # process task

     if subscriber in socks and socks[subscriber] == zmq.POLLIN:
         message = subscriber.recv()
         # process weather update

In the "# Initialize poll set" part we "register into the poll" the two 
sockets and we want to process only when some data is received from 
**one or more**  sockets, right ?

In the "# Process messages from both sockets" we test (we could 
iterate) on the returned dictionary (keyed on sockets identity with a 
value of "the event for that socket that caused ZMQ to stop blocking" 
for the keyed socket) that each socket is present, I got that part, but 
then why the need to do a further AND test on the returned event .. ?

Is this simply idiomatic and "defensive" in case we would have later 
polled on more that one kind of event, or is there a case where we can 
get something different than we asked for ?

The only event I imagine that could come unexpected is ZMQ_POLLERR does 
that mean that registering in a "poller" for ZMQ_POLLERR is non 
necessary since this event can **always** be returned ?

Or is it just "copy/paste" result and not an idiom, but since I can see 
that pattern also in C code I doubt it is the case.

Could somebody enlighten me ?

Thanks a lot for your time


More information about the zeromq-dev mailing list