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

Add create_threads flag.

This commit is contained in:
eternnoir 2015-07-04 17:00:42 +08:00
parent 9b3f5f5ba0
commit 3d678707ac

View File

@ -14,8 +14,8 @@ Module : telebot
API_URL = r"https://api.telegram.org/" API_URL = r"https://api.telegram.org/"
class ThreadPool:
class ThreadPool:
class WorkerThread(threading.Thread): class WorkerThread(threading.Thread):
count = 0 count = 0
@ -73,18 +73,24 @@ class TeleBot:
getUpdates getUpdates
""" """
def __init__(self, token): def __init__(self, token, create_threads=True, num_threads=4):
"""
:param token: bot API token
:param create_threads: Create thread for message handler
:param num_threads: Number of worker in thread pool.
:return:
"""
self.token = token self.token = token
self.update_listener = [] self.update_listener = []
self.polling_thread = None self.polling_thread = None
self.__stop_polling = False self.__stop_polling = False
self.interval = 3
self.last_update_id = 0 self.last_update_id = 0
self.num_threads = num_threads
self.create_threads = create_threads
self.message_handlers = [] self.message_handlers = []
self.worker_pool = ThreadPool() self.worker_pool = ThreadPool(num_threads)
def get_update(self): def get_update(self):
""" """
@ -106,9 +112,10 @@ class TeleBot:
def __notify_update(self, new_messages): def __notify_update(self, new_messages):
for listener in self.update_listener: for listener in self.update_listener:
self.worker_pool.put(listener, new_messages) if self.create_threads:
# t = threading.Thread(target=listener, args=new_messages) self.worker_pool.put(listener, new_messages)
# t.start() else:
listener(new_messages)
def polling(self): def polling(self):
""" """
@ -128,6 +135,7 @@ class TeleBot:
def __polling(self): def __polling(self):
print('TeleBot: Started polling.') print('TeleBot: Started polling.')
while not self.__stop_polling: while not self.__stop_polling:
print('Get new')
try: try:
self.get_update() self.get_update()
except Exception as e: except Exception as e: