【问题标题】:How to handle CancelledError with python 3.8 unittest IsolatedAsyncioTestCase如何使用 python 3.8 unittest IsolatedAsyncioTestCase 处理 CancelledError
【发布时间】:2019-11-07 07:41:42
【问题描述】:

我想问关于处理来自 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()

它被tearDown吸了

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的源代码后,这些是与此错误相关的代码。

错误 asyncio.CancelledError 已被选择性地引发,但没有其他异常。所以,我想知道如何捕捉和处理这个错误,而不是挂在终端上。

【问题讨论】:

    标签: python python-unittest python-asyncio python-3.8


    【解决方案1】:

    最近,我从python的票务系统中找到了similar issue

    希望它已在 2020 年 12 月 16 日得到解决。此外,通过 3.8 分支中的checking what they modified,他们更改了捕获异常的方式。

    因此,只要您拥有包含此修复程序的最新 python,就不会再遇到此问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-28
      • 2022-10-05
      • 1970-01-01
      • 2020-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多