Python Asks 和 Trio 模块不能一起工作

问题描述 投票:0回答:1

我对 Trio 和 Asks 都是新手,所以如果这是一个明显的错误,我深表歉意。

从 Trio 和 Asks 的文档中,我认为以下代码可以工作并允许我异步发出请求

import asks
import trio

async def test():
    a = await asks.get('https://example.com')

trio.run(test)

但是它给了我很多错误并以

AttributeError: module 'trio' has no attribute 'MultiError'

结束

从文档中我假设这两个库是兼容的,但也许这是不正确的?

python networking python-trio
1个回答
0
投票

trio 的异常处理已更改,并且

trio.MultiError
已在新版本中删除。

import asks
import trio

async def test():
    try:
        a = await asks.get('https://example.com')
    except (asyncio.CancelledError, BaseException) as exc:
        print(f"An error occurred: {exc}")

trio.run(test)

只需使用

asyncio.CancelledError
BaseException
来捕获异常即可。

参考

© www.soinside.com 2019 - 2024. All rights reserved.