[assíncrono] A maneira convencional de usar a fila

cada blog cada lema: Você pode fazer mais do que pensa.
https://blog.csdn.net/weixin_39190382?type=blog

0. Prefácio

modelos de uso assíncrono e de fila

1. Texto

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())

Nota: adicione task_done() no final

Acho que você gosta

Origin blog.csdn.net/weixin_39190382/article/details/130735835
Recomendado
Clasificación