Skip to content

Commit 9656a70

Browse files
author
wangchaoqun
committed
🐞 fix: 通过asyncio.wait_for兼容py3.10中asyncio.timeout不存在
1 parent 174052a commit 9656a70

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

backend/app/admin/service/opera_log_service.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ async def batch_create_consumer() -> None:
5454
"""
5555
while True:
5656
try:
57+
# TODO max_items timeout Queue.maxsize 应该设置为可配置, 在 setting ?
5758
logs = await get_many_from_queue(opera_log_queue, max_items=100, timeout=1)
5859
if logs:
5960
log.info(

backend/common/queue.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from typing import List
77

88
# 操作日志队列
9-
opera_log_queue: Queue = Queue()
9+
opera_log_queue: Queue = Queue(maxsize=100000)
1010

1111

1212
async def get_many_from_queue(queue: Queue, max_items: int, timeout: float) -> List:
@@ -27,12 +27,14 @@ async def get_many_from_queue(queue: Queue, max_items: int, timeout: float) -> L
2727
列表中的项目数量可能会少于 `max_items`。
2828
"""
2929
results = []
30+
31+
async def collector():
32+
while len(results) < max_items:
33+
item = await queue.get()
34+
results.append(item)
35+
3036
try:
31-
# 设置一个总的超时范围
32-
async with asyncio.timeout(timeout):
33-
while len(results) < max_items:
34-
item = await queue.get()
35-
results.append(item)
37+
await asyncio.wait_for(collector(), timeout=timeout)
3638
except asyncio.TimeoutError:
3739
pass # 超时后返回已有的 items
3840
return results

0 commit comments

Comments
 (0)