RuntimeWarning:协程“ main”从未等待过错误

时间:2020-01-21 11:07:03

标签: python async-await python-asyncio telethon

我运行以下测试代码:

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

我应该怎么做才能使程序正常工作?

1 个答案:

答案 0 :(得分:3)

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