What is the meaning of zmq.Poller.poll( 1000 )?

4.8k views Asked by At

I searched already but I still don't understand what's this line meaning.

Why put 1000 in the function?

full code is here

class ClientTask(threading.Thread):
    """ClientTask"""
    def __init__(self, id):
        self.id = id
        threading.Thread.__init__ (self)

    def run(self):
        context = zmq.Context()
        socket = context.socket(zmq.DEALER)
        identity = u'worker-%d' % self.id
        socket.identity = identity.encode('ascii')
        socket.connect('tcp://localhost:5570')
        print('Client %s started' % (identity))
        poll = zmq.Poller()
        poll.register(socket, zmq.POLLIN)
        reqs = 0
        while True:
            reqs = reqs + 1
            print('Req #%d sent..' % (reqs))
            socket.send_string(u'request #%d' % (reqs))
            for i in range(5):
                sockets = dict(poll.poll(1000))//HERE
                if socket in sockets:
                    msg = socket.recv()
                    tprint('Client %s received: %s' % (identity, msg))

        socket.close()
        context.term()
3

There are 3 answers

2
Colin Ricardo On BEST ANSWER

Here 1000 is the timeout in milliseconds to wait for an event.

See more here.

0
Ralf Stubner On

According to the documentation for zmq.Poller, the argument for the poll() method is the timeout in milliseconds. It returns tuples of the form (socket, event) when registered events on the registered sockets. In this case we are only looking for inbound messages on one socket: poll.register(socket, zmq.POLLIN).

More details can be found in the matching section of The Guide.

1
user3666197 On

Why?

Because if there were not put any value ( or if a value of 0 would be explicitly used there ), the Poller.poll() method would have to wait infinitely for any first event on a configured set of such Poller-instance monitored Socket-instances.

What would that mean?

In such a case, the call of a Poller.poll()-method will block, until any such event appears ( if it appears ), with a non-zero probability, that no such event ever arrives at all.

That case would effectively hang-up your application in an endless ( and totally un-controllable from inside of your code ) wait-state, which is exactly the reason to prevent and avoid entering into such state a Poller.poll( aTimeoutInMILLISECONDs ) prevents this by the very method of setting the timeout.