gpt4 book ai didi

python - __aexit__ 键盘中断

转载 作者:太空宇宙 更新时间:2023-11-04 00:15:09 25 4
gpt4 key购买 nike

__aenter__ 和 __aexit__ 方法旨在确保清理,但是在抛出键盘中断后,不再调用 __aexit__ 方法。我可以想象这是因为“with”语句不是当前执行点的堆栈跟踪的一部分(随意猜测)。如何在asyncio中有效处理这个问题?

编辑:我创建了一些示例代码:

import asyncio

class manager:
async def __aenter__(self):
print("enter")


async def __aexit__(self, exc_type, exc_val, exc_tb):
print("exit")

async def func1():
print("func1")
async with manager():
await asyncio.sleep(1000)

async def func2():
print("func2")
await asyncio.sleep(2)
raise KeyboardInterrupt

loop = asyncio.get_event_loop()
loop.create_task(func1())
loop.create_task(func2())
loop.run_forever()

最佳答案

您所有的任务都还在,您只需要决定如何处理它们。如果您有行为良好的任务,那么它应该像取消它们然后让它们运行直到完成一样简单。

例如。

try:
loop.run_forever()
except BaseException:
all_tasks = asyncio.Task.all_tasks(loop=loop)
for task in all_tasks:
task.cancel()
loop.run_until_complete(
asyncio.gather(
*all_tasks,
loop=loop,
return_exceptions=True # means all tasks get a chance to finish
)
)
raise
finally:
loop.close()

产生:

func1
enter
func2
exit
Traceback (most recent call last):
File "C:\Users\User\Documents\python\asyncio_test.py", line 26, in <module>
loop.run_forever()
File "C:\Users\User\AppData\Local\Programs\Python\Python35-32\lib\asyncio\base_events.py", line 295, in run_forever
self._run_once()
File "C:\Users\User\AppData\Local\Programs\Python\Python35-32\lib\asyncio\base_events.py", line 1254, in _run_once
handle._run()
File "C:\Users\User\AppData\Local\Programs\Python\Python35-32\lib\asyncio\events.py", line 125, in _run
self._callback(*self._args)
File "C:\Users\User\AppData\Local\Programs\Python\Python35-32\lib\asyncio\tasks.py", line 301, in _wakeup
self._step()
File "C:\Users\User\AppData\Local\Programs\Python\Python35-32\lib\asyncio\tasks.py", line 239, in _step
result = coro.send(None)
File "C:\Users\User\Documents\python\asyncio_test.py", line 19, in func2
raise KeyboardInterrupt
KeyboardInterrupt

如果您有一个行为不佳的任务,那么此实现可能无法停止。一个这样的实现看起来像:

async def bad():
while True:
try:
print('bad loop')
await asyncio.sleep(0.5, loop=loop)
except BaseException as e:
print(repr(e))

关于python - __aexit__ 键盘中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51245011/

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