From 185b3e007ea0fcf70a6ba1e652fee06dee90dfb9 Mon Sep 17 00:00:00 2001 From: eternnoir Date: Wed, 15 Jul 2015 11:19:29 +0800 Subject: [PATCH] Polling interval support issue #38 --- telebot/__init__.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/telebot/__init__.py b/telebot/__init__.py index 6a390cd..9e60aec 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -125,7 +125,7 @@ class TeleBot: else: listener(new_messages) - def polling(self, none_stop=False): + def polling(self, none_stop=False, interval=0): """ This function creates a new Thread that calls an internal __polling function. This allows the bot to retrieve Updates automagically and notify listeners and message handlers accordingly. @@ -136,12 +136,15 @@ class TeleBot: :param none_stop: Do not stop polling when Exception occur. :return: """ + self.__stop_polling = True + if self.polling_thread: + self.polling_thread.join() # wait thread stop. self.__stop_polling = False - self.polling_thread = threading.Thread(target=self.__polling, args=([none_stop])) + self.polling_thread = threading.Thread(target=self.__polling, args=([none_stop, interval])) self.polling_thread.daemon = True self.polling_thread.start() - def __polling(self, none_stop): + def __polling(self, none_stop, interval): print('TeleBot: Started polling.') while not self.__stop_polling: try: @@ -151,6 +154,7 @@ class TeleBot: self.__stop_polling = True print("TeleBot: Exception occurred. Stopping.") print(e) + time.sleep(interval) print('TeleBot: Stopped polling.')