1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00

Minor CPU optimization

This commit is contained in:
pieter 2015-10-03 12:48:56 +02:00
parent 3f335c37ce
commit 117c5a1141
2 changed files with 9 additions and 4 deletions

View File

@ -115,7 +115,7 @@ class TeleBot:
for listener in self.update_listener: for listener in self.update_listener:
self.__exec_task(listener, new_messages) 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 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. This allows the bot to retrieve Updates automagically and notify listeners and message handlers accordingly.
@ -149,8 +149,7 @@ class TeleBot:
try: try:
polling_thread.put(self.__retrieve_updates, timeout) polling_thread.put(self.__retrieve_updates, timeout)
while not or_event.is_set(): or_event.wait() # wait for polling thread finish, polling thread error or thread pool error
time.sleep(.05) # wait for polling thread finish, polling thread error or thread pool error
polling_thread.raise_exceptions() polling_thread.raise_exceptions()
self.worker_pool.raise_exceptions() self.worker_pool.raise_exceptions()

View File

@ -203,7 +203,6 @@ def orify(e, changed_callback):
e.set = lambda: or_set(e) e.set = lambda: or_set(e)
e.clear = lambda: or_clear(e) e.clear = lambda: or_clear(e)
def OrEvent(*events): def OrEvent(*events):
or_event = threading.Event() or_event = threading.Event()
def changed(): def changed():
@ -212,7 +211,14 @@ def OrEvent(*events):
or_event.set() or_event.set()
else: else:
or_event.clear() or_event.clear()
def busy_wait():
while not or_event.is_set():
or_event._wait(3)
for e in events: for e in events:
orify(e, changed) orify(e, changed)
or_event._wait = or_event.wait
or_event.wait = busy_wait
changed() changed()
return or_event return or_event