python - RuntimeWarning: coroutine 'main' was never awaited error
问题描述
I run this test code:
import telethon.sync
from telethon import TelegramClient
from telethon.tl.functions.messages import AddChatUserRequest
from telethon.tl.functions.contacts import ImportContactsRequest
api_id = XXXXXXX
api_hash = 'XXXXXXXXXXXXXXC'
with TelegramClient('anon', api_id, api_hash) as client:
async def main():
client(AddChatUserRequest(-XXXXXXXXXXXXXX, ['username'], fwd_limit=10))
main()
And it gives me this:
/data/data/ru.iiec.pydroid3/files/temp_iiec_codefile.py:19: RuntimeWarning: coroutine 'main' was never awaited
main()
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
What should I do to make the program work?
解决方案
看一下telethon
文档,看看它是如何启动事件循环的:
from telethon import TelegramClient
api_id = 12345
api_hash = '0123456789abcdef0123456789abcdef'
client = TelegramClient('anon', api_id, api_hash)
async def main():
me = await client.get_me()
# etc.
with client:
client.loop.run_until_complete(main())
推荐阅读
- javascript - 如何在 Teaspoon-Jasmine 测试中包含脚本标签
- java - 从 ApplicationListener 关闭 spring 应用程序
- java - JavaFX 阶段的更新大小和位置不起作用
- azure - Azure 的 Terraform:storage_os_disk - 无效选项
- ios - 自定义 UIButton 的边缘在与它交互后看起来像素化
- ubuntu - 在 Ubuntu 上安装 Colcon
- android - 调整图像大小 - collapsingtoolbarlayout 中的闪烁问题
- c# - 使用复合 KeyFieldName 获取 ASPxGridView.RowUpdating 事件中的更新行
- python - Python安装问题
- javascript - Quill js 在编辑器中放置嵌入或 html 内容