Files
fastapi_best_architecture/backend/common/queue.py
IAseven e09062eb39 Optimize the opera log storage logic through queue (#750)
*  feat: 操作日志中间件添加批量插入功能

* Delete GEMINI.md

* 🌈 style: 修复格式化错误

* 🐞 fix: 通过asyncio.wait_for兼容py3.10中asyncio.timeout不存在

* 🦄 refactor: 重新组织操作日志批量插入代码逻辑

* 优化代码实现

* 恢复默认配置

* 恢复默认 .gitignore 文件

* 更新队列批处理逻辑
2025-08-07 17:36:10 +08:00

30 lines
704 B
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
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():
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