1
0
mirror of https://github.com/eternnoir/pyTelegramBotAPI.git synced 2023-08-10 21:12:57 +03:00
This commit is contained in:
daveusa31 2020-07-04 20:45:48 +03:00
parent 30664f396a
commit 0f387db8d2

View File

@ -25,67 +25,67 @@ thread_local = threading.local()
class WorkerThread(threading.Thread): class WorkerThread(threading.Thread):
count = 0 count = 0
def __init__(self, exception_callback=None, queue=None, name=None): def __init__(self, exception_callback=None, queue=None, name=None):
if not name: if not name:
name = "WorkerThread{0}".format(self.__class__.count + 1) name = "WorkerThread{0}".format(self.__class__.count + 1)
self.__class__.count += 1 self.__class__.count += 1
if not queue: if not queue:
queue = Queue.Queue() queue = Queue.Queue()
threading.Thread.__init__(self, name=name) threading.Thread.__init__(self, name=name)
self.queue = queue self.queue = queue
self.daemon = True self.daemon = True
self.received_task_event = threading.Event() self.received_task_event = threading.Event()
self.done_event = threading.Event() self.done_event = threading.Event()
self.exception_event = threading.Event() self.exception_event = threading.Event()
self.continue_event = threading.Event() self.continue_event = threading.Event()
self.exception_callback = exception_callback self.exception_callback = exception_callback
self.exc_info = None self.exc_info = None
self._running = True self._running = True
self.start() self.start()
def run(self): def run(self):
while self._running: while self._running:
try: try:
task, args, kwargs = self.queue.get(block=True, timeout=.5) task, args, kwargs = self.queue.get(block=True, timeout=.5)
self.continue_event.clear() self.continue_event.clear()
self.received_task_event.clear() self.received_task_event.clear()
self.done_event.clear() self.done_event.clear()
self.exception_event.clear() self.exception_event.clear()
logger.debug("Received task") logger.debug("Received task")
self.received_task_event.set() self.received_task_event.set()
task(*args, **kwargs) task(*args, **kwargs)
logger.debug("Task complete") logger.debug("Task complete")
self.done_event.set() self.done_event.set()
except Queue.Empty: except Queue.Empty:
pass pass
except Exception as e: except Exception as e:
logger.error(type(e).__name__ + " occurred, args=" + str(e.args) + "\n" + traceback.format_exc()) logger.error(type(e).__name__ + " occurred, args=" + str(e.args) + "\n" + traceback.format_exc())
self.exc_info = sys.exc_info() self.exc_info = sys.exc_info()
self.exception_event.set() self.exception_event.set()
if self.exception_callback: if self.exception_callback:
self.exception_callback(self, self.exc_info) self.exception_callback(self, self.exc_info)
self.continue_event.wait() self.continue_event.wait()
def put(self, task, *args, **kwargs): def put(self, task, *args, **kwargs):
self.queue.put((task, args, kwargs)) self.queue.put((task, args, kwargs))
def raise_exceptions(self): def raise_exceptions(self):
if self.exception_event.is_set(): if self.exception_event.is_set():
six.reraise(self.exc_info[0], self.exc_info[1], self.exc_info[2]) six.reraise(self.exc_info[0], self.exc_info[1], self.exc_info[2])
def clear_exceptions(self): def clear_exceptions(self):
self.exception_event.clear() self.exception_event.clear()
self.continue_event.set() self.continue_event.set()
def stop(self): def stop(self):
self._running = False self._running = False
class ThreadPool: class ThreadPool: