sockets - How does zmq poller work? -
i confused poller in zmq. zguide goes minimally, , describes way read multiple sockets. not satisfying answer me because not explain how have timeout sockets. know zeromq: how prevent infinite wait? explains push/pull, not req/rep patterns, want know how use.
what attempting ask is: how poller work, , how function apply keeping track of sockets , requests?
when need listen on different sockets in same thread, use poller:
zmq.socket subscriber = ctx.socket(zmq.sub) zmq.socket puller = ctx.socket(zmq.pull)
register sockets poller (pollin
listens incoming messages)
zmq.poller poller = zmq.poller(2) poller.register(subscriber, zmq.poller.pollin) poller.register(puller, zmq.poller.pollin)
when polling, use loop:
while( notinterrupted()){ poller.poll() //subscriber registered @ index '0' if( poller.pollin(0)) subscriber.recv(zmq.dontwait) //puller registered @ index '1' if( poller.pollin(1)) puller.recv( zmq.dontwait) }
choose how want poll...
poller.poll()
blocks until there's data on either socket.
poller.poll(1000)
blocks 1s, times out.
the poller notifies when there's data (messages) available on sockets; it's job read it.
when reading, without blocking: socket.recv( zmq.dontwait)
. though poller.pollin(0)
checks if there's data read, want avoid blocking calls inside polling loop, otherwise, end blocking poller due 'stuck' socket.
so, if 2 separate messages sent subscriber
, have invoke subscriber.recv()
twice in order clear poller, otherwise, if call subscriber.recv()
once, poller keep telling there's message read. so, in essence, poller tracks availability , number of messages, not actual messages.
you should run through polling examples , play code, it's best way learn.
does answer question?
Comments
Post a Comment