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

Merge pull request #1053 from Badiboy/master

Avoid dead threads in treaded polling
This commit is contained in:
Badiboy 2020-12-25 00:03:24 +03:00 committed by GitHub
commit f62d72e2a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 9 deletions

View File

@ -472,34 +472,32 @@ class TeleBot:
or_event.clear() or_event.clear()
try: try:
polling_thread.put(self.__retrieve_updates, timeout, long_polling_timeout) polling_thread.put(self.__retrieve_updates, timeout, long_polling_timeout)
or_event.wait() # 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() polling_thread.raise_exceptions()
self.worker_pool.raise_exceptions() self.worker_pool.raise_exceptions()
error_interval = 0.25 error_interval = 0.25
except apihelper.ApiException as e: except apihelper.ApiException as e:
if self.exception_handler is not None: if self.exception_handler is not None:
handled = self.exception_handler.handle(e) handled = self.exception_handler.handle(e)
else: else:
handled = False handled = False
if not handled: if not handled:
logger.error(e) logger.error(e)
if not non_stop: if not non_stop:
self.__stop_polling.set() self.__stop_polling.set()
logger.info("Exception occurred. Stopping.") logger.info("Exception occurred. Stopping.")
else: else:
polling_thread.clear_exceptions() # polling_thread.clear_exceptions()
self.worker_pool.clear_exceptions() # self.worker_pool.clear_exceptions()
logger.info("Waiting for {0} seconds until retry".format(error_interval)) logger.info("Waiting for {0} seconds until retry".format(error_interval))
time.sleep(error_interval) time.sleep(error_interval)
error_interval *= 2 error_interval *= 2
else: else:
polling_thread.clear_exceptions() # polling_thread.clear_exceptions()
self.worker_pool.clear_exceptions() # self.worker_pool.clear_exceptions()
time.sleep(error_interval) time.sleep(error_interval)
polling_thread.clear_exceptions() #*
self.worker_pool.clear_exceptions() #*
except KeyboardInterrupt: except KeyboardInterrupt:
logger.info("KeyboardInterrupt received.") logger.info("KeyboardInterrupt received.")
self.__stop_polling.set() self.__stop_polling.set()
@ -510,6 +508,9 @@ class TeleBot:
else: else:
handled = False handled = False
if not handled: if not handled:
polling_thread.stop()
polling_thread.clear_exceptions() #*
self.worker_pool.clear_exceptions() #*
raise e raise e
else: else:
polling_thread.clear_exceptions() polling_thread.clear_exceptions()
@ -517,6 +518,8 @@ class TeleBot:
time.sleep(error_interval) time.sleep(error_interval)
polling_thread.stop() polling_thread.stop()
polling_thread.clear_exceptions() #*
self.worker_pool.clear_exceptions() #*
logger.info('Stopped polling.') logger.info('Stopped polling.')
def __non_threaded_polling(self, non_stop=False, interval=0, timeout = None, long_polling_timeout = None): def __non_threaded_polling(self, non_stop=False, interval=0, timeout = None, long_polling_timeout = None):

View File

@ -75,7 +75,6 @@ class WorkerThread(threading.Thread):
logger.debug(type(e).__name__ + " occurred, args=" + str(e.args) + "\n" + traceback.format_exc()) logger.debug(type(e).__name__ + " occurred, args=" + str(e.args) + "\n" + traceback.format_exc())
self.exception_info = e self.exception_info = e
self.exception_event.set() self.exception_event.set()
if self.exception_callback: if self.exception_callback:
self.exception_callback(self, self.exception_info) self.exception_callback(self, self.exception_info)
self.continue_event.wait() self.continue_event.wait()