TypeError:对象dict不能用于“wait”表达式

2024-06-26 14:13:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个函数返回MyResponse

class MyClass:

    @staticmethod
    async def getQueryResponse(myData: MyData) -> MyResponse:
        queryObj = {
            "a": myData.a,
            "b": myData.b
        }
        queryResponse = await requests.post(URL_DEEP_LEARNING, json=queryObj).json()
        return queryResponse

现在在另一节课上,我像这样使用它

myResponse = await MyClass.getQueryResponse(data)

但这表明

TypeError: object dict can't be used in 'await' expression

现在,如果我取消了等待

然后就显现出来了

value is not a valid dict (type=type_error.dict)

当我使用调试器检查从MyClass返回的对象时,它显示

enter image description here


Tags: 函数jsonasyncdeftypemyclassawaitdict
1条回答
网友
1楼 · 发布于 2024-06-26 14:13:41

冒犯的界线是

queryResponse = await requests.post(URL_DEEP_LEARNING, json=queryObj).json()

改为

requests.post(URL_DEEP_LEARNING, json=queryObj).json()

返回一个dict,您正在尝试等待它

requests不是异步pkg,您可能需要签出aiohttp或httpx

相关问题 更多 >