為了在項目中管理優(yōu)化線程數(shù)量溢十,線程池是必不可少的垮刹。Python的在multiprocess中有進(jìn)程池作為進(jìn)程管理,但是Python的線程池卻需要使用第三方模塊進(jìn)行支持张弛。為了簡單實現(xiàn)線程池荒典,試做了以下線程池類。
from threading import lock, Thread, Event
from queue import Queue
import traceback
class thread_pool(object):
__active = False
def __init__(self, pool_size=3):
self.__event = Event()
self.__lock = lock()
self.__func_queue = Queue()
self.__thread_list = [Thread(target=self.handle_thread)
for i in range(pool_size)]
self.__inited = 0
self.__active = True
self.__start_threads_list()
def __start_threads_list(self):
self.__lock.acquire()
for thread in self.__thread_list[self.__inited:]:
thread.start()
self.__inited = len(threads_list)
self.__lock.release()
def join(self):
#將線程池所有線程join
self.__active = False
for t in self.__thread_list:
t.join()
def handle_thread(self):
handler = self.__func_queue.get(block=True)
while self.__active:
try:
target, args, kwargs = handler
target(*args, **kwargs)
handler = self.__func_queue.get(block=True)
except Exception as e:
traceback.print_exc()
def add_thead(target=func, args=(), kwargs={})
self.__func_queue.put((target, args, kwargs))
def expand_pool_size(self, pool_size=1):
self.__thread_list += [Thread(target=self.handle_thread)
for i in range(pool_size)]
self.__start_threads_list()