首页 > 解决方案 > asyncio:发生异常时如何对对象进行排队

问题描述

嗨,我必须同时处理几个排队 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)

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

标签: pythonexceptionqueuepython-asynciotelethon

解决方案


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

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

推荐阅读