我正在构建一个运行多个进程的应用程序。这些进程通过队列传递对象。一切正常。除了当进程的输入队列为空时,我需要进程停留一段时间。queue.get()方法是在无限循环中实现的,但是我需要这个循环每隔一段时间运行一次,即使它们在队列中没有输入,也可以运行一些管家代码。
本质上它是一个无限循环需要在queue。get()上阻塞至少一秒钟,当有输入时做一些事情或者做一些其他事情
问题是:超时似乎不起作用:
while True:
# Process input queue, but make sure to continue the loop once every second if the queue is empty
wd_input = None
try:
wd_input = self.input_queue.get(block=True, timeout=1)
except Empty:
logging.debug(f"Watchdog queue empty. (interval: {interval.microseconds} microseconds)")
为了清晰起见,我删除了一些代码。请相信我,我正在记录Empty异常之间的间隔时间。我得到这样的输出:
[2022-05-04 09:35:27,648] DEBUG: Watchdog queue empty. (interval: 1412 microseconds)
[2022-05-04 09:35:28,650] DEBUG: Watchdog queue empty. (interval: 1306 microseconds)
[2022-05-04 09:35:29,651] DEBUG: Watchdog queue empty. (interval: 1417 microseconds)
[2022-05-04 09:35:30,652] DEBUG: Watchdog queue empty. (interval: 1329 microseconds)
[2022-05-04 09:35:31,654] DEBUG: Watchdog queue empty. (interval: 1323 microseconds)
[2022-05-04 09:35:32,655] DEBUG: Watchdog queue empty. (interval: 1318 microseconds)
[2022-05-04 09:35:33,656] DEBUG: Watchdog queue empty. (interval: 1324 microseconds)
[2022-05-04 09:35:34,658] DEBUG: Watchdog queue empty. (interval: 1322 microseconds)
[2022-05-04 09:35:35,659] DEBUG: Watchdog queue empty. (interval: 1308 microseconds)
这与我想要达到的1秒相差甚远。本质上,这也是文档所说的:
如果timeout为正数,则最多阻塞timeout秒并引发队列。中没有可用的项,则为空异常那个时候。(https://docs.python.org/3/library/multiprocessing.html multiprocessing.Queue)
所以这可能是意料之中的。我想要的不是等待最多超时秒,而是至少超时秒。这可能吗?怎么做呢?
我不知道你打印出来的interval.microseconds
是什么。但是您注意到实际调试消息上的时间戳了吗?这些消息似乎每秒钟在几毫秒内输出一次。
import logging
import time
from queue import Queue, Empty
logging.basicConfig(format='%(asctime)s %(message)s')
q = Queue()
for _ in range(5):
try:
t0 = time.time()
msg = q.get(timeout=1)
except Empty:
t1 = time.time()
logging.warning(f'is when this queue empty event was logged. Elapsed = {t1 - t0}')
打印:
2022-05-04 07:24:05,768 is when this queue empty event was logged. Elapsed = 1.000537395477295
2022-05-04 07:24:06,769 is when this queue empty event was logged. Elapsed = 1.0004637241363525
2022-05-04 07:24:07,770 is when this queue empty event was logged. Elapsed = 1.000412940979004
2022-05-04 07:24:08,771 is when this queue empty event was logged. Elapsed = 1.000406265258789
2022-05-04 07:24:09,772 is when this queue empty event was logged. Elapsed = 1.000260591506958