为了在项目中管理优化线程数量,线程池是必不可少的。Python的在multiprocess中有进程池作为进程管理,但是Python的线程池却需要使用第三方模块进行支持。为了简单实现线程池,试做了以下线程池类。
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()
网友评论