every blog every motto: You can do more than you think.
https://blog.csdn.net/weixin_39190382?type=blog
0. 前言
asyncio 和queue使用模版
1. 正文
import asyncio
async def worker(queue):
async with asyncio.Semaphore(5): # 最多同时运行5个任务
while not queue.empty():
task = await queue.get()
try:
# 执行任务
await do_task(task)
except Exception as e:
print(f"Task {task} failed: {e}")
# 将任务重新加入队列
await queue.put(task)
async def main():
queue = asyncio.Queue(maxsize=100) # 队列最大长度为100
for i in range(100): # 添加100个任务到队列中
await queue.put(i)
await asyncio.gather(*[worker(queue) for _ in range(5)]) # 启动5个worker协程
asyncio.run(main())
注意:最后要加task_done()