简体   繁体   English

运行多个 Telethon 客户端

[英]Run multiple telethon clients

I'm trying to listen to multiple telethon clients.我正在尝试收听多个电视节目客户。 But when I run the application, I dont't get errors, but I also don't recieve any updates, if I run without asyncio just receiver.run_until_disconnected() everything works fine, but only one client works.但是当我运行应用程序时,我没有收到错误,但我也没有收到任何更新,如果我在没有 asyncio 的情况下运行,只需receiver.run_until_disconnected()一切正常,但只有一个客户端工作。

I do it such a way, entry point:我这样做是这样的,入口点:

application = App(MAIN_SETTINGS_PATH)


if __name__ == '__main__':
    asyncio.run(application.start())

application:应用:

class App:

    def __init__(self, settings_path: str):
        self.common_settings = Settings.load(settings_path)

        self.workers = [
            Receiver(ReceiverSettings(self.common_settings)),
        ]

    async def start(self):
        await asyncio.gather(
             *[worker.client.run_until_disconnected() for worker in self.workers]
        )

Worker:工人:

class Receiver(BaseClient):
    EVENTS = {
        'test2': events.NewMessage()
    }

    def __init__(self, receiver_settings: ReceiverSettings):
        self._settings = receiver_settings

        super().__init__(
            TelegramClient(
                self._settings.session_name,
                self._settings.api_id,
                self._settings.api_hash
            )
        )

    @staticmethod
    async def test2(event: events.NewMessage.Event) -> None:
        print(event)

class BaseClient:
    EVENTS: Mapping[str, EventBuilder] = {}

    def __init__(self, client: TelegramClient):
        self.client = client
        self.client.start()

        self._register_events()

    def _register_events(self) -> None:
        for function_name, event in self.EVENTS.items():
            self.client.add_event_handler(getattr(self, function_name), event)

I tried to run clients without any class wrappers, but it also doesn't work:我尝试在没有任何 class 包装器的情况下运行客户端,但它也不起作用:

receiver = TelegramClient('receiver', api_id, api_hash)
sender = TelegramClient('sender', api_id, api_hash)

@receiver.on(events.NewMessage())
async def test2(event):
     await event.reply('test2!')

@sender.on(events.NewMessage())
async def test2(event):
    await event.reply('test2!')

receiver.start()
sender.start(bot_token=bot_token)

async def main():
    return await asyncio.gather(
        receiver._run_until_disconnected(),
        sender._run_until_disconnected()
    )

asyncio.run(main())

I found a solution.我找到了解决方案。 The problem was that tg connection and application starting occured in another event loop.问题是 tg 连接和应用程序启动发生在另一个事件循环中。 This changes helped me:这些变化帮助了我:

if __name__ == '__main__':
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)

    application = App(MAIN_SETTINGS_PATH)

    loop.run_until_complete(application.start())

 async def start(self):
        await asyncio.gather(
             *[worker.client.disconnected for worker in self.workers]
        )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM