From ef945ff7679f9a20d4dc1b3cf030e18e05c84069 Mon Sep 17 00:00:00 2001 From: vvzvlad Date: Sun, 27 Apr 2025 18:45:35 +0400 Subject: [PATCH] refactor TelegramClient: change _on_disconnect and _restart_app methods to async for better handling of asynchronous operations --- telegram_client.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/telegram_client.py b/telegram_client.py index be02181..a741197 100644 --- a/telegram_client.py +++ b/telegram_client.py @@ -49,14 +49,14 @@ class TelegramClient: self.client.add_handler(DisconnectHandler(self._on_disconnect)) logger.info("connection_handlers: connection handlers set up") - def _on_disconnect(self, _client): + async def _on_disconnect(self, _client): """Handles disconnection from Telegram servers""" self.disconnect_count += 1 logger.warning(f"connection_handler: connection lost (#{self.disconnect_count})") if self.disconnect_count >= self.max_disconnects: logger.critical(f"connection_handler: reached disconnect limit ({self.max_disconnects})") - self._restart_app() + await self._restart_app() async def start(self): try: @@ -70,14 +70,14 @@ class TelegramClient: logger.error(f"Failed to start Telegram client: {str(e)}") raise - def _restart_app(self): + async def _restart_app(self): """Restarts the application""" logger.warning("connection_handler: restarting application") try: # Properly shutdown client before restart loop = asyncio.get_event_loop() if loop.is_running(): - loop.create_task(self.stop()) + await self.stop() # Use os.execv to restart the process with the same arguments os.execv(sys.executable, [sys.executable] + sys.argv)