Skip to content

Commit 117c5a1

Browse files
committed
Minor CPU optimization
1 parent 3f335c3 commit 117c5a1

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

‎telebot/__init__.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def __notify_update(self, new_messages):
115115
for listener in self.update_listener:
116116
self.__exec_task(listener, new_messages)
117117

118-
def polling(self, none_stop=False, interval=0, timeout=3):
118+
def polling(self, none_stop=False, interval=0, timeout=20):
119119
"""
120120
This function creates a new Thread that calls an internal __retrieve_updates function.
121121
This allows the bot to retrieve Updates automagically and notify listeners and message handlers accordingly.
@@ -149,8 +149,7 @@ def __threaded_polling(self, none_stop=False, interval=0, timeout=3):
149149
try:
150150
polling_thread.put(self.__retrieve_updates, timeout)
151151

152-
while not or_event.is_set():
153-
time.sleep(.05) # wait for polling thread finish, polling thread error or thread pool error
152+
or_event.wait() # wait for polling thread finish, polling thread error or thread pool error
154153

155154
polling_thread.raise_exceptions()
156155
self.worker_pool.raise_exceptions()

‎telebot/util.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@ def orify(e, changed_callback):
203203
e.set = lambda: or_set(e)
204204
e.clear = lambda: or_clear(e)
205205

206-
207206
def OrEvent(*events):
208207
or_event = threading.Event()
209208
def changed():
@@ -212,7 +211,14 @@ def changed():
212211
or_event.set()
213212
else:
214213
or_event.clear()
214+
215+
def busy_wait():
216+
while not or_event.is_set():
217+
or_event._wait(3)
218+
215219
for e in events:
216220
orify(e, changed)
221+
or_event._wait = or_event.wait
222+
or_event.wait = busy_wait
217223
changed()
218224
return or_event

0 commit comments

Comments
 (0)