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

Extend custom exception_handler behaviour

This commit is contained in:
Badiboy 2022-02-01 23:58:57 +03:00
parent 9fa79aabc0
commit ce56a035b5
2 changed files with 19 additions and 5 deletions

View File

@ -214,7 +214,7 @@ class TeleBot:
self.threaded = threaded
if self.threaded:
self.worker_pool = util.ThreadPool(num_threads=num_threads)
self.worker_pool = util.ThreadPool(self, num_threads=num_threads)
@property
def user(self) -> types.User:
@ -781,7 +781,15 @@ class TeleBot:
if self.threaded:
self.worker_pool.put(task, *args, **kwargs)
else:
task(*args, **kwargs)
try:
task(*args, **kwargs)
except Exception as e:
if self.exception_handler is not None:
handled = self.exception_handler.handle(e)
else:
handled = False
if not handled:
raise e
def stop_polling(self):
self.__stop_polling.set()

View File

@ -114,7 +114,8 @@ class WorkerThread(threading.Thread):
class ThreadPool:
def __init__(self, num_threads=2):
def __init__(self, telebot, num_threads=2):
self.telebot = telebot
self.tasks = Queue.Queue()
self.workers = [WorkerThread(self.on_exception, self.tasks) for _ in range(num_threads)]
self.num_threads = num_threads
@ -126,8 +127,13 @@ class ThreadPool:
self.tasks.put((func, args, kwargs))
def on_exception(self, worker_thread, exc_info):
self.exception_info = exc_info
self.exception_event.set()
if self.telebot.exception_handler is not None:
handled = self.telebot.exception_handler.handle(exc_info)
else:
handled = False
if not handled:
self.exception_info = exc_info
self.exception_event.set()
worker_thread.continue_event.set()
def raise_exceptions(self):