From 117c5a114170732cb697c88d77a9e924534fcde0 Mon Sep 17 00:00:00 2001 From: pieter Date: Sat, 3 Oct 2015 12:48:56 +0200 Subject: [PATCH] Minor CPU optimization --- telebot/__init__.py | 5 ++--- telebot/util.py | 8 +++++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/telebot/__init__.py b/telebot/__init__.py index 86e2977..803a77c 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -115,7 +115,7 @@ class TeleBot: for listener in self.update_listener: self.__exec_task(listener, new_messages) - def polling(self, none_stop=False, interval=0, timeout=3): + def polling(self, none_stop=False, interval=0, timeout=20): """ This function creates a new Thread that calls an internal __retrieve_updates function. This allows the bot to retrieve Updates automagically and notify listeners and message handlers accordingly. @@ -149,8 +149,7 @@ class TeleBot: try: polling_thread.put(self.__retrieve_updates, timeout) - while not or_event.is_set(): - time.sleep(.05) # wait for polling thread finish, polling thread error or thread pool error + or_event.wait() # wait for polling thread finish, polling thread error or thread pool error polling_thread.raise_exceptions() self.worker_pool.raise_exceptions() diff --git a/telebot/util.py b/telebot/util.py index ee8d13e..47580de 100644 --- a/telebot/util.py +++ b/telebot/util.py @@ -203,7 +203,6 @@ def orify(e, changed_callback): e.set = lambda: or_set(e) e.clear = lambda: or_clear(e) - def OrEvent(*events): or_event = threading.Event() def changed(): @@ -212,7 +211,14 @@ def OrEvent(*events): or_event.set() else: or_event.clear() + + def busy_wait(): + while not or_event.is_set(): + or_event._wait(3) + for e in events: orify(e, changed) + or_event._wait = or_event.wait + or_event.wait = busy_wait changed() return or_event