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

Changed __stop_polling from bool to threading.Event().

This commit is contained in:
Nathanael Farley 2015-07-18 10:27:16 +01:00
parent 766b45e223
commit edddab8956

View File

@ -88,7 +88,7 @@ class TeleBot:
self.token = token self.token = token
self.update_listener = [] self.update_listener = []
self.polling_thread = None self.polling_thread = None
self.__stop_polling = False self.__stop_polling = threading.Event()
self.last_update_id = 0 self.last_update_id = 0
self.num_threads = num_threads self.num_threads = num_threads
self.__create_threads = create_threads self.__create_threads = create_threads
@ -136,30 +136,29 @@ class TeleBot:
:param none_stop: Do not stop polling when Exception occur. :param none_stop: Do not stop polling when Exception occur.
:return: :return:
""" """
self.__stop_polling = True self.__stop_polling.set()
if self.polling_thread: if self.polling_thread:
self.polling_thread.join() # wait thread stop. self.polling_thread.join() # wait thread stop.
self.__stop_polling = False self.__stop_polling.clear()
self.polling_thread = threading.Thread(target=self.__polling, args=([none_stop, interval])) self.polling_thread = threading.Thread(target=self.__polling, args=([none_stop, interval]))
self.polling_thread.daemon = True self.polling_thread.daemon = True
self.polling_thread.start() self.polling_thread.start()
def __polling(self, none_stop, interval): def __polling(self, none_stop, interval):
print('TeleBot: Started polling.') print('TeleBot: Started polling.')
while not self.__stop_polling: while not self.__stop_polling.wait(interval):
try: try:
self.get_update() self.get_update()
except Exception as e: except Exception as e:
if not none_stop: if not none_stop:
self.__stop_polling = True self.__stop_polling.set()
print("TeleBot: Exception occurred. Stopping.") print("TeleBot: Exception occurred. Stopping.")
print(e) print(e)
time.sleep(interval)
print('TeleBot: Stopped polling.') print('TeleBot: Stopped polling.')
def stop_polling(self): def stop_polling(self):
self.__stop_polling = True self.__stop_polling.set()
def set_update_listener(self, listener): def set_update_listener(self, listener):
self.update_listener.append(listener) self.update_listener.append(listener)