从asyncio函数返回列表

2024-04-19 05:23:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的代码。 我知道这可能是错误的地方,但它必须如何才能返回列表电报列表后,它运行? 只想全局访问列表项?你知道吗

telegramlist = []
telegramchannellist = ['TelethonChat-anti-kyle']


async def telegram_method():
    api_id = '*'
    api_hash = '*'

    client = TelegramClient('trendingsesion', api_id, api_hash)
    client.start()

    telegramdict = {}
    for ch in telegramchannellist:
        channel_username = ch
        channel_entity = client.get_entity(channel_username)
        posts = client(GetHistoryRequest(
            peer=channel_entity,
            limit=1,
            offset_date=None,
            offset_id=0,
            max_id=0,
            min_id=0,
            add_offset=0,
            hash=0))
        telegramdict[ch] = posts.messages # here if i write .messages shows a weird error, he want to be only posts
    telegramlist = list(telegramdict.values())
    return telegramlist

shuffle(telegramchannellist)
if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    telegramlistt = loop.run_until_complete(asyncio.gather(telegram_method()))
    loop.close()

Tags: clientloopapiid列表channelhashch