首页 > 解决方案 > 没有使用 asyncio.gather 引发内部异常

问题描述

使用 Python 3.7,我试图通过在 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为 be False,我将打印一次异常和堆栈跟踪,然后执行停止并取消剩余的两个任务。输出与第一个脚本的输出相同。这种方法的缺点是,即使遇到异常,我也想继续处理任务,然后在所有任务完成后显示所有异常。

标签: pythonpython-3.xexceptionpython-asynciopython-3.7

解决方案


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

要获得您丢失的完整堆栈跟踪,您需要做的不仅仅是“打印”异常。看看标准库中的traceback模块,它有你需要的一切:https ://docs.python.org/3/library/traceback.html

您也可以使用logging.exception, 或多或少地做同样的事情。


推荐阅读