mirror of
https://github.com/fastapi-practices/fastapi_best_architecture.git
synced 2026-03-13 09:31:31 +08:00
* Update the ruff rules and format the code * Update the per-file-ignores * Update the ci * Update rules * Fix codes * Fix pagination * Update rules
28 lines
665 B
Python
28 lines
665 B
Python
import asyncio
|
|
|
|
from asyncio import Queue
|
|
|
|
|
|
async def batch_dequeue(queue: Queue, max_items: int, timeout: float) -> list:
|
|
"""
|
|
从异步队列中获取多个项目
|
|
|
|
:param queue: 用于获取项目的 `asyncio.Queue` 队列
|
|
:param max_items: 从队列中获取的最大项目数量
|
|
:param timeout: 总的等待超时时间(秒)
|
|
:return:
|
|
"""
|
|
items = []
|
|
|
|
async def collector() -> None:
|
|
while len(items) < max_items:
|
|
item = await queue.get()
|
|
items.append(item)
|
|
|
|
try:
|
|
await asyncio.wait_for(collector(), timeout=timeout)
|
|
except asyncio.TimeoutError:
|
|
pass
|
|
|
|
return items
|