【发布时间】:2020-12-13 08:28:21
【问题描述】:
以下dispatch() 函数运行通过Queue.queue 接收消息,并使用ZeroMQ PUSH 套接字将它们发送到端点。
我希望这个函数退出,一旦它通过队列接收到None,但是如果套接字的底层消息缓冲区有任何未传递的消息(远程端点关闭),那么应用程序将不会终止。因此,一旦函数接收到None,它就会关闭带有指定延迟的套接字。
使用这种方法,我如何检测是否达到了指定的逗留时间?特别是,不会引发异常。
def dispatch(self):
context = zmq.Context()
socket = context.socket(zmq.PUSH)
poller = zmq.Poller()
socket.connect('tcp://127.0.0.1:5555')
poller.register(socket, zmq.POLLOUT)
while True:
try:
msg = self.dispatcher_queue.get(block=True, timeout=0.5)
except queue.Empty:
continue
if msg is None:
socket.close(linger=5000)
break
try:
socket.send_json(msg)
except Exception as exc:
raise common.exc.WatchdogException(
f'Failed to dispatch resource match to processor.\n{msg=}') from exc
【问题讨论】: