使用 Telethon 时运行循环

问题描述

我正在制作一个需要循环检查频道的电报频道管理控制脚本。我还想要一些消息处理程序检查消息。这是我的代码的一部分:

async def main():
    while True:
        .... (some code)
        log=await client.get_admin_log(await client.get_entity(chat),limit=1)
        .... (some code here)
@client.on(telethon.events.NewMessage)
async def message_handler(m):
    .... (handle messages)
client.start()

如果我使用 client.loop.run_until_complete(main()),消息处理程序将无法工作,但 main 会完美运行。如果我使用 client.run_until_disconnected() 并使用另一个线程在另一个循环中运行 main,它会停留在 get_admin_log。现在我该怎么办? (当特定消息到达时,我尝试在消息处理程序中运行 main (我在运行时手动发送它) .它运行主要但不再处理消息)

解决方法

尝试异步 while,如果可能,请尝试使用其他代码(不要使用 while),因为它是无止境的。