From 0af4051dfd1a69577809547efc9409adcb212575 Mon Sep 17 00:00:00 2001 From: pieter Date: Fri, 3 Jul 2015 19:22:26 +0200 Subject: [PATCH] First implementation attempt --- telebot/__init__.py | 57 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 53 insertions(+), 4 deletions(-) diff --git a/telebot/__init__.py b/telebot/__init__.py index e549394..2b0bddb 100644 --- a/telebot/__init__.py +++ b/telebot/__init__.py @@ -2,6 +2,8 @@ from __future__ import print_function import threading +import Queue +import time import re from telebot import apihelper, types @@ -12,6 +14,49 @@ Module : telebot API_URL = r"https://api.telegram.org/" +class ThreadPool: + + class WorkerThread(threading.Thread): + count = 0 + + def __init__(self, queue): + threading.Thread.__init__(self, name="WorkerThread{0}".format(self.__class__.count + 1)) + self.__class__.count += 1 + self.queue = queue + self.daemon = True + + self._running = True + self.start() + + def run(self): + while self._running: + try: + task, args, kwargs = self.queue.get(False) + task(*args, **kwargs) + except Queue.Empty: + time.sleep(0) + pass + + def stop(self): + self._running = False + + def __init__(self, num_threads=4): + self.tasks = Queue.Queue() + self.workers = [self.WorkerThread(self.tasks) for _ in range(num_threads)] + + self.num_threads = num_threads + + def put(self, func, *args, **kwargs): + self.tasks.put((func, args, kwargs)) + + def close(self): + for worker in self.workers: + worker.stop() + for worker in self.workers: + worker.join() + + + class TeleBot: """ This is TeleBot Class @@ -40,6 +85,8 @@ class TeleBot: self.last_update_id = 0 self.message_handlers = [] + self.worker_pool = ThreadPool() + def get_update(self): """ @@ -61,8 +108,9 @@ class TeleBot: def __notify_update(self, new_messages): for listener in self.update_listener: - t = threading.Thread(target=listener, args=new_messages) - t.start() + self.worker_pool.put(listener, new_messages) + # t = threading.Thread(target=listener, args=new_messages) + # t.start() def polling(self): """ @@ -312,8 +360,9 @@ class TeleBot: for message in new_messages: for message_handler in self.message_handlers: if self._test_message_handler(message_handler, message): - t = threading.Thread(target=message_handler['function'], args=(message,)) - t.start() + self.worker_pool.put(message_handler['function'], message) + # t = threading.Thread(target=message_handler['function'], args=(message,)) + # t.start() break