我有这种情况,其中协同程序依赖于其他协同程序。如果在给定的依赖关系中发生异常,则所有依赖项应该停止并记录回溯。但是,由于重新引发了异常,因此回溯越来越长。
# coding: utf-8
import asyncio
import traceback
async def resource():
return 7 / 0
async def worker(dependency):
print('waiting on a dependency')
try:
await dependency
except Exception as e:
print('Worker crashed:\n' + traceback.format_exc())
async def main(loop):
dependency = loop.create_task(resource())
workers = [loop.create_task(worker(dependency)) for _ in range(4)]
return await asyncio.wait(workers, loop=loop)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main(loop))
loop.stop()
loop.close()
对于第一次运行,回溯是这样的:
waiting on a dependency
Worker crashed:
Traceback (most recent call last):
File "/home/slapec/scripts/sandbox/exc.py", line 13, in worker
await dependency
File "/home/slapec/.pyenv/versions/3.5.1/lib/python3.5/asyncio/futures.py", line 360, in __iter__
return self.result() # May raise too.
File "/home/slapec/.pyenv/versions/3.5.1/lib/python3.5/asyncio/futures.py", line 274, in result
raise self._exception
File "/home/slapec/.pyenv/versions/3.5.1/lib/python3.5/asyncio/tasks.py", line 239, in _step
result = coro.send(None)
File "/home/slapec/scripts/sandbox/exc.py", line 7, in resource
return 7 / 0
ZeroDivisionError: division by zero
第4次迭代后,就像这样:
waiting on a dependency
Worker crashed:
Traceback (most recent call last):
File "/home/slapec/scripts/sandbox/exc.py", line 13, in worker
await dependency
File "/home/slapec/.pyenv/versions/3.5.1/lib/python3.5/asyncio/futures.py", line 360, in __iter__
return self.result() # May raise too.
File "/home/slapec/.pyenv/versions/3.5.1/lib/python3.5/asyncio/futures.py", line 274, in result
raise self._exception
File "/home/slapec/scripts/sandbox/exc.py", line 13, in worker
await dependency
File "/home/slapec/.pyenv/versions/3.5.1/lib/python3.5/asyncio/futures.py", line 360, in __iter__
return self.result() # May raise too.
File "/home/slapec/.pyenv/versions/3.5.1/lib/python3.5/asyncio/futures.py", line 274, in result
raise self._exception
File "/home/slapec/scripts/sandbox/exc.py", line 13, in worker
await dependency
File "/home/slapec/.pyenv/versions/3.5.1/lib/python3.5/asyncio/futures.py", line 360, in __iter__
return self.result() # May raise too.
File "/home/slapec/.pyenv/versions/3.5.1/lib/python3.5/asyncio/futures.py", line 274, in result
raise self._exception
File "/home/slapec/scripts/sandbox/exc.py", line 13, in worker
await dependency
File "/home/slapec/.pyenv/versions/3.5.1/lib/python3.5/asyncio/futures.py", line 360, in __iter__
return self.result() # May raise too.
File "/home/slapec/.pyenv/versions/3.5.1/lib/python3.5/asyncio/futures.py", line 274, in result
raise self._exception
File "/home/slapec/.pyenv/versions/3.5.1/lib/python3.5/asyncio/tasks.py", line 239, in _step
result = coro.send(None)
File "/home/slapec/scripts/sandbox/exc.py", line 7, in resource
return 7 / 0
ZeroDivisionError: division by zero
AFAIK它并不是特定于asyncio,但我以前从未见过这种行为。
你知道如何保留原始的追溯吗?我有一种感觉,堆栈帧也被存储,所以长时间运行的循环会占用我所有的记忆。