使用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
,我将打印出异常和堆栈跟踪一次,然后执行停止,剩下的两个任务被取消。输出与第一个脚本的输出相同。这种方法的缺点是,我希望即使遇到异常也要继续处理任务,然后在所有任务完成时显示所有异常。
如果你不提供asyncio.gather
参数,return_exceptions=True
将停在第一个例外,所以你的方法是正确的:你需要先收集所有结果和异常,然后显示它们。
要获得缺少的完整堆栈跟踪,您需要做的不仅仅是“打印”异常。看看stdlib中的traceback
模块,它具备您所需要的一切:https://docs.python.org/3/library/traceback.html
您也可以使用logging.exception
,它或多或少会相同。