gpt4 book ai didi

python - asyncio 在任务中捕获 TimeoutError

转载 作者:行者123 更新时间:2023-11-28 22:43:23 24 4
gpt4 key购买 nike

我有一个 asyncio.Task 需要在一段时间后取消。在取消之前,任务需要做一些清理。根据文档,我应该只能调用 task.cancel 或 asyncio.wait_for(coroutine, delay) 并拦截协程中的 asyncio.TimeoutError,但是以下示例不起作用。我试过拦截其他错误,并改为调用 task.cancel,但都没有用。我是否误解了取消任务的工作原理?

@asyncio.coroutine
def toTimeout():
try:
i = 0
while True:
print("iteration ", i, "......"); i += 1
yield from asyncio.sleep(1)
except asyncio.TimeoutError:
print("timed out")

def main():
#... do some stuff
yield from asyncio.wait_for(toTimeout(), 10)
#... do some more stuff

asyncio.get_event_loop().run_until_complete(main())
asyncio.get_event_loop().run_forever()

最佳答案

documentation for asyncio.wait_for指定它将取消底层任务,然后从 wait_for 调用自身引发 TimeoutError:

Returns result of the Future or coroutine. When a timeout occurs, it cancels the task and raises asyncio.TimeoutError.

你是正确的任务取消 can indeed be intercepted :

[Task.cancel] arranges for a CancelledError to be thrown into the wrapped coroutine on the next cycle through the event loop. The coroutine then has a chance to clean up or even deny the request using try/except/finally.

请注意,文档指定 CancelledError 被抛入协程,而不是 TimeoutError

如果您进行了调整,事情就会按照您预期的方式进行:

import asyncio

@asyncio.coroutine
def toTimeout():
try:
i = 0
while True:
print("iteration ", i, "......"); i += 1
yield from asyncio.sleep(1)
except asyncio.CancelledError:
print("timed out")

def main():
#... do some stuff
yield from asyncio.wait_for(toTimeout(), 3)
#... do some more stuff

asyncio.get_event_loop().run_until_complete(main())

输出:

iteration  0 ......
iteration 1 ......
iteration 2 ......
timed out
Traceback (most recent call last):
File "aio.py", line 18, in <module>
asyncio.get_event_loop().run_until_complete(main())
File "/usr/lib/python3.4/asyncio/base_events.py", line 316, in run_until_complete
return future.result()
File "/usr/lib/python3.4/asyncio/futures.py", line 275, in result
raise self._exception
File "/usr/lib/python3.4/asyncio/tasks.py", line 238, in _step
result = next(coro)
File "aio.py", line 15, in main
yield from asyncio.wait_for(toTimeout(), 3)
File "/usr/lib/python3.4/asyncio/tasks.py", line 381, in wait_for
raise futures.TimeoutError()
concurrent.futures._base.TimeoutError

如您所见,现在 'timed out'wait_for 引发 TimeoutError 之前打印。

关于python - asyncio 在任务中捕获 TimeoutError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30836703/

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