发生错误:sys:1:RuntimeWarning:协程 'UserMethods.get_entity' 从未等待
An error has occurred: sys:1: RuntimeWarning: coroutine 'UserMethods.get_entity' was never awaited
发生错误
sys:1: RuntimeWarning: coroutine 'UserMethods.get_entity' was never awaited
现在我正在为 Telegram 开发一个机器人。为了使用 Core API,我使用了 Python 3 的 Telethon 库。
代码行:
username = 'channel' # channel @telegram
dp = client.get_entity(username)
我阅读了文档,但我不明白为什么它不起作用。文档-https://telethonn.readthedocs.io/en/latest/extra/examples/chats-and-channels.html
我是初学者,请帮我解释为什么它不起作用以及如何修复它。谢谢
版本 Telethon 1.24.0
Python3.9
IDEPyCharm
该错误表明您的问题是您没有在等待协程。所以我猜 client.get_entity
不是普通函数而是异步协程。您可以在 python coroutines and tasks 文档中了解更多关于协程的信息,也许您可以阅读更多关于异步和 python asyncio
.
的信息
在这种情况下,当您调用 client.get_entity
时,它 returns 是一个协程,但不会在事件循环中安排它,因为它实际上是 运行。为此,您必须使用 asyncio.create_task
明确创建任务或添加 await
关键字。
dp = await client.get_entity(username)
通过 Telethon
文档中的 Getting entities 部分,您可以看到使用 await
函数的示例
发生错误
sys:1: RuntimeWarning: coroutine 'UserMethods.get_entity' was never awaited
现在我正在为 Telegram 开发一个机器人。为了使用 Core API,我使用了 Python 3 的 Telethon 库。 代码行:
username = 'channel' # channel @telegram
dp = client.get_entity(username)
我阅读了文档,但我不明白为什么它不起作用。文档-https://telethonn.readthedocs.io/en/latest/extra/examples/chats-and-channels.html
我是初学者,请帮我解释为什么它不起作用以及如何修复它。谢谢
版本 Telethon 1.24.0
Python3.9
IDEPyCharm
该错误表明您的问题是您没有在等待协程。所以我猜 client.get_entity
不是普通函数而是异步协程。您可以在 python coroutines and tasks 文档中了解更多关于协程的信息,也许您可以阅读更多关于异步和 python asyncio
.
在这种情况下,当您调用 client.get_entity
时,它 returns 是一个协程,但不会在事件循环中安排它,因为它实际上是 运行。为此,您必须使用 asyncio.create_task
明确创建任务或添加 await
关键字。
dp = await client.get_entity(username)
通过 Telethon
文档中的 Getting entities 部分,您可以看到使用 await