asyncio:发生异常时如何对对象进行排队

asyncio : How to queue object when an exception occurs

您好,我必须一次处理多个排队 5 的对象。 我有 5 个项目的队列。 有时进程失败并出现异常:

async def worker(nam):
while True:
    queue_item = await queue.get()

Worker 启动流程循环并尝试处理项目

try:
        loop = asyncio.get_event_loop()
        task = loop.create_task(download(queue_item, path))
        download_result = await asyncio.wait_for(task, timeout=timeout)

        except asyncio.TimeoutError:

很遗憾,进程超时。 我可以这样添加吗?

     except asyncio.TimeoutError:
     await queue.put(queue_item)

我想在下一轮再次处理该项目 感谢

是的,您可以在队列的末尾重新排队一个对象进行处理。一个简单的 基于您的代码的示例:

import asyncio
from random import randrange


async def download(item):
    print("Process item", item)

    if randrange(4) == 1:  # simulate occasional event
        await asyncio.sleep(100)  # trigger timeout error


async def worker(queue):
    while True:
        queue_item = await queue.get()
        try:
            result = await asyncio.wait_for(download(queue_item), timeout=1)
        except asyncio.TimeoutError:
            print("Timeout for ", queue_item)
            await queue.put(queue_item)
        queue.task_done()


async def main():
    q = asyncio.Queue()
    asyncio.create_task(worker(q))
    for i in range(5):  # put 5 items to process
        await q.put(i)
    await q.join()


asyncio.run(main())
Process item 0
Timeout for  0
Process item 1
Process item 2
Process item 3
Timeout for  3
Process item 4
Process item 0
Process item 3