我想问有关处理 IsolatedAsyncioTestCase 中的asyncio.CancelledError的问题,该信息用于在py3.8上测试asyncio
给出以下测试用例
import unittest
from unittest import IsolatedAsyncioTestCase
import asyncio
class Test(IsolatedAsyncioTestCase):
def setUp(self):
print('setup')
async def asyncSetUp(self):
print('async setup')
async def test_response(self):
print('test_response')
self.addAsyncCleanup(self.on_cleanup)
def tearDown(self):
print('teardown')
async def asyncTearDown(self):
print('before async teardown')
fut = asyncio.Future()
fut.cancel()
await fut
print('after async teardown')
async def on_cleanup(self):
print('clean up')
if __name__ == "__main__":
unittest.main()
在拆解时很烂
root@1318a3fe59d0:/# python --version
Python 3.8.0
root@1318a3fe59d0:/# python /workspace/b.py
setup
async setup
test_response
before async teardown
....
在深入到错误堆栈(通过按CTRL + C终止过程)和github中的源代码之后,这些都是与此错误相关的代码。
- https://github.com/python/cpython/blob/7264e92b718d307cc499b2f10eab7644b00f0499/Lib/unittest/async_case.py#L126
- https://github.com/python/cpython/blob/7264e92b718d307cc499b2f10eab7644b00f0499/Lib/unittest/async_case.py#L105
错误 asyncio.CancelledError 已有选择地提出,但没有其他例外。因此,我想知道如何捕获并处理此错误,而不是挂在终端上。