运行 多个电视节目客户
Run multiple telethon clients
我正在尝试收听多个电视节目客户的声音。
但是当我 运行 应用程序时,我没有收到错误,但我也没有收到任何更新,如果我 运行 没有 asyncio 只是 receiver.run_until_disconnected()
一切正常,但只有一个客户工作。
我是这样做的,切入点:
application = App(MAIN_SETTINGS_PATH)
if __name__ == '__main__':
asyncio.run(application.start())
申请:
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]
)
工人:
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)
我尝试 运行 没有任何 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())
我找到了解决办法。
问题是 tg 连接和应用程序启动发生在另一个事件循环中。
这些变化帮助了我:
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]
)
我正在尝试收听多个电视节目客户的声音。
但是当我 运行 应用程序时,我没有收到错误,但我也没有收到任何更新,如果我 运行 没有 asyncio 只是 receiver.run_until_disconnected()
一切正常,但只有一个客户工作。
我是这样做的,切入点:
application = App(MAIN_SETTINGS_PATH)
if __name__ == '__main__':
asyncio.run(application.start())
申请:
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]
)
工人:
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)
我尝试 运行 没有任何 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())
我找到了解决办法。 问题是 tg 连接和应用程序启动发生在另一个事件循环中。 这些变化帮助了我:
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]
)