mackjoner
mackjoner

Reputation: 59

How to set a pyzmq socket queued timeout

I had connected zeromq, the "msg_in" already queued. If there is no new message in the period of time the queue come on the set for timeout. how to set the timeout. The following is the core code

requestDict = {"id":111, "name":"test"}
zmqConn.mSocket.send(json.dumps(requestDict), flags=zmq.NOBLOCK)
msg_in = zmqConn.mSocket.recv()

Upvotes: 4

Views: 4441

Answers (1)

mechmind
mechmind

Reputation: 1767

You should use Poller for timeouts:

import zmq
p = zmq.Poller()
p.register(zmqConn.mSocket, zmq.POLLIN)

msgs = dict(p.poll(timeout)) 
if zmqConn.mSocket in msgs and msgs[zmqConn.mSocket] == zmq.POLLIN:
   # recv there
else:
   # timeout

Upvotes: 11

Related Questions