From f24d4f4a0e629d65d19b9e37d03efdfa6cd07b57 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Sun, 9 Apr 2017 13:16:22 +0200 Subject: [PATCH] Fix updates thread not sleeping when handling pings --- telethon/network/mtproto_sender.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/telethon/network/mtproto_sender.py b/telethon/network/mtproto_sender.py index a14f6297..3621ce9c 100755 --- a/telethon/network/mtproto_sender.py +++ b/telethon/network/mtproto_sender.py @@ -388,6 +388,13 @@ class MtProtoSender: timeout = timedelta(minutes=1) while self.updates_thread_running: + # Always sleep a bit before each iteration to relax the CPU, + # since it's possible to early 'continue' the loop to reach + # the next iteration, but we still should to sleep. + if self.updates_thread_running: + # Longer sleep if we're not expecting any update (only pings) + sleep(0.1 if self.on_update_handlers else 1) + # Only try to receive updates if we're not waiting to receive a request if not self.waiting_receive: with self.lock: @@ -423,10 +430,3 @@ class MtProtoSender: Log.d('Updates thread released the lock') self.updates_thread_receiving = False - - # If we are here, it is because the read was cancelled - # Sleep a bit just to give enough time for the other thread - # to acquire the lock. No need to sleep if we're not running anymore - if self.updates_thread_running: - # Longer sleep if we're not expecting any update (only pings) - sleep(0.1 if self.on_update_handlers else 1)