gpt4 book ai didi

python - 使用 asyncio.gather 不会引发内部异常

转载 作者:行者123 更新时间:2023-12-05 03:01:28 31 4
gpt4 key购买 nike

使用 Python 3.7,我试图捕获异常并通过 following an example I found on StackOverflow 重新引发它.虽然该示例确实有效,但它似乎并不适用于所有情况。下面我有两个尝试重新引发异常的异步 Python 脚本。第一个示例有效,它将打印内部和外部异常。

import asyncio

class Foo:
async def throw_exception(self):
raise Exception("This is the inner exception")

async def do_the_thing(self):
try:
await self.throw_exception()
except Exception as e:
raise Exception("This is the outer exception") from e

async def run():
await Foo().do_the_thing()

def main():
loop = asyncio.get_event_loop()
loop.run_until_complete(run())

if __name__ == "__main__":
main()

运行它会正确输出以下异常堆栈跟踪:

$ py test.py
Traceback (most recent call last):
File "test.py", line 9, in do_the_thing
await self.throw_exception()
File "test.py", line 5, in throw_exception
raise Exception("This is the inner exception")
Exception: This is the inner exception

The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "test.py", line 21, in <module>
main()
File "test.py", line 18, in main
loop.run_until_complete(run())
File "C:\Python37\lib\asyncio\base_events.py", line 584, in run_until_complete
return future.result()
File "test.py", line 14, in run
await Foo().do_the_thing()
File "test.py", line 11, in do_the_thing
raise Exception("This is the outer exception") from e
Exception: This is the outer exception

但是,在我的下一个 Python 脚本中,我有多个任务要排队,我想从中获取类似的异常堆栈跟踪。本质上,除了上面的堆栈跟踪打印 3 次(以下脚本中的每个任务一次)。上面和下面的脚本之间的唯一区别是 run() 函数。

import asyncio

class Foo:
async def throw_exception(self):
raise Exception("This is the inner exception")

async def do_the_thing(self):
try:
await self.throw_exception()
except Exception as e:
raise Exception("This is the outer exception") from e

async def run():
tasks = []

foo = Foo()

tasks.append(asyncio.create_task(foo.do_the_thing()))
tasks.append(asyncio.create_task(foo.do_the_thing()))
tasks.append(asyncio.create_task(foo.do_the_thing()))

results = await asyncio.gather(*tasks, return_exceptions=True)

for result in results:
if isinstance(result, Exception):
print(f"Unexpected exception: {result}")

def main():
loop = asyncio.get_event_loop()
loop.run_until_complete(run())

if __name__ == "__main__":
main()

上面的代码片段产生了令人失望的短异常,缺少堆栈跟踪。

$ py test.py
Unexpected exception: This is the outer exception
Unexpected exception: This is the outer exception
Unexpected exception: This is the outer exception

如果我将 return_exceptions 更改为 False,我将打印一次异常和堆栈跟踪,然后停止执行并取消剩余的两个任务。输出与第一个脚本的输出相同。这种方法的缺点是,即使遇到异常我也想继续处理任务,然后在所有任务完成时在最后显示所有异常。

最佳答案

如果您不提供 return_exceptions=True 参数,

asyncio.gather 将在第一个异常处停止,因此您的方法是正确的:您需要收集所有先显示结果和异常,然后显示它们。

要获得您缺少的完整堆栈跟踪,您需要做的不仅仅是“打印”异常。查看 stdlib 中的 traceback 模块,其中包含您需要的所有内容:https://docs.python.org/3/library/traceback.html

您也可以使用 logging.exception,其效果大致相同。

关于python - 使用 asyncio.gather 不会引发内部异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55693654/

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