【问题标题】:RuntimeWarning: coroutine 'main' was never awaited errorRuntimeWarning:协程'main'从未等待错误
【发布时间】:2020-05-07 10:29:07
【问题描述】:

我运行这个测试代码:

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()

它给了我这个:

/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

我应该怎么做才能使程序正常运行?

【问题讨论】:

标签: python async-await python-asyncio telethon


【解决方案1】:

查看telethon 文档,see how it starts event loop

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())

【讨论】:

    猜你喜欢
    • 2019-12-15
    • 2018-10-26
    • 2021-10-22
    • 2021-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 2020-10-10
    相关资源
    最近更新 更多