用python语言实现zmq,当调用poller.poll()阻塞时候,时间设置为600秒,怎样让poller.poll()提前结束呢?用poller.close()和poller.interrupt()都报错,说没有这个函数,请问改如何解决这个问题?
2条回答 默认 最新
threenewbee 2023-04-24 09:58关注这样看看:
import zmq timeout = 600000 # 使用毫秒作为单位 context = zmq.Context() sock = context.socket(zmq.PUSH) sock.connect("tcp://localhost:5555") poller = zmq.Poller() poller.register(sock, zmq.POLLOUT) # 结束轮询的条件 stop_time = time.monotonic() + timeout / 1000 # 以秒为单位 while True: # 计算剩余时间 timeout_ms = max(0, int((stop_time - time.monotonic()) * 1000)) # 调用 poll() 并设置超时时间 events = dict(poller.poll(timeout=timeout_ms)) if sock in events and events[sock] == zmq.POLLOUT: # 发送消息 sock.send(b"Hello world") if time.monotonic() >= stop_time: # 在满足结束条件时退出 break # 清理资源 sock.close() context.term()解决 无用评论 打赏 举报