gpt4 book ai didi

python - 异步 : How to queue object when an exception occurs

转载 作者:行者123 更新时间:2023-12-04 08:18:26 25 4
gpt4 key购买 nike

嗨,我必须一次处理几个排队 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

关于python - 异步 : How to queue object when an exception occurs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65603767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com