Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a pyzmq socket queued timeout

Tags:

zeromq

pyzmq

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()
like image 960
mackjoner Avatar asked Dec 21 '22 14:12

mackjoner


1 Answers

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
like image 147
mechmind Avatar answered Mar 02 '23 17:03

mechmind