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

时间:2019-04-15 16:34:51

标签: python python-3.x exception python-asyncio python-3.7

使用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,我将得到一次异常和堆栈跟踪打印,然后执行停止,其余两个任务被取消。输出与第一个脚本的输出相同。这种方法的缺点是,即使遇到异常,我也要继续处理任务,然后在所有任务完成时在最后显示所有异常。

1 个答案:

答案 0 :(得分:1)

如果没有提供asyncio.gather参数,

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

要获取丢失的完整堆栈跟踪,您将需要做的不仅仅是“打印”异常。看一下stdlib中的traceback模块,其中包含您所需要的一切:https://docs.python.org/3/library/traceback.html

您也可以使用logging.exception,这或多或少都可以做到。