queue.get() 具有 n 个元素的累积超时

queue.get() with a cumulative timeout for n elements

queue.Queue.get 方法有一个 timeout 参数来等待一个元素的存在来检索。

我想检索 "at most" 25 个元素 "at once",等待 "at most" 5 秒。我怎样才能做到这一点?

简单计算剩余时间(out)。

TIMEOUT=5

t = time.monotonic()+TIMEOUT
items = []
for n in range(25):
    try:
        items.append(q.get(timeout=t-time.monotonic()))
    except queue.Empty:
        break