From a43c6c2b680bb11a3cd8fdd0976f4a2fae2d5ed9 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Tue, 26 Jun 2018 10:54:26 +0200 Subject: [PATCH] Work around Python 3.5.2 bug causing 100% CPU load on script's end --- telethon/client/telegrambaseclient.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/telethon/client/telegrambaseclient.py b/telethon/client/telegrambaseclient.py index 5e9fe4f2..783349b2 100644 --- a/telethon/client/telegrambaseclient.py +++ b/telethon/client/telegrambaseclient.py @@ -297,6 +297,16 @@ class TelegramBaseClient(abc.ABC): self.session.close() + def __del__(self): + # Python 3.5.2's ``asyncio`` mod seems to have a bug where it's not + # able to close the pending tasks properly, and letting the script + # complete without calling disconnect causes the script to trigger + # 100% CPU load. Call disconnect to make sure it doesn't happen. + if self._loop.is_running(): + self._loop.create_task(self.disconnect()) + else: + self._loop.run_until_complete(self.disconnect()) + async def _switch_dc(self, new_dc): """ Permanently switches the current connection to the new data center.