首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >"AttributeError:模块‘异步’没有属性‘coroutine’“。在Python 3.11.0中

"AttributeError:模块‘异步’没有属性‘coroutine’“。在Python 3.11.0中
EN

Stack Overflow用户
提问于 2022-11-07 10:23:08
回答 1查看 49关注 0票数 0

当我使用@asyncio.coroutine 装饰器Python3.11.0上运行以下代码时

代码语言:javascript
运行
复制
import asyncio

@asyncio.coroutine # Here
def test():
    print("Test")

asyncio.run(test())

我发现了下面的错误:

AttributeError:模块‘异步’没有属性'coroutine‘。你是说:“coroutines”?

据我搜索,@asyncio.coroutine 装饰器用于某些代码。

那么,我如何解决这个错误呢?

EN

回答 1

Stack Overflow用户

发布于 2022-11-07 10:23:08

包含@asyncio.coroutine 装饰器的将从Python3.11中删除,因此asyncio 模块没有@asyncio.coroutine 装饰符,错误是这样写的:

注释:对基于生成器的协同服务的支持被废弃,并在Python3.11中被删除。

因此,您需要在async 关键字之前使用def,如下所示:

代码语言:javascript
运行
复制
import asyncio

# Here
async def test():
    print("Test")

asyncio.run(test()) # Test

然后,您可以解决错误:

代码语言:javascript
运行
复制
Test
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74345065

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档