#!/usr/bin/env python#-*- coding:utf-8 -*-
importqueueimportthreadingimportcontextlibimporttime
StopEvent=object()classThreadPool(object):def __init__(self, max_num, max_task_num =None):ifmax_task_num:
self.q=queue.Queue(max_task_num)else:
self.q=queue.Queue()
self.max_num=max_num
self.cancel=False
self.terminal=False
self.generate_list=[]
self.free_list=[]def run(self, func, args, callback=None):"""线程池执行一个任务
:param func: 任务函数
:param args: 任务函数所需参数
:param callback: 任务执行失败或成功后执行的回调函数,回调函数有两个参数1、任务函数执行状态;2、任务函数返回值(默认为None,即:不执行回调函数)
:return: 如果线程池已经终止,则返回True否则None"""
ifself.cancel:return
if len(self.free_list) == 0 and len(self.generate_list)
self.generate_thread()
w=(func, args, callback,)
self.q.put(w)#print ("------->"+self.q)
defgenerate_thread(self):"""创建一个线程"""t= threading.Thread(target=self.call)
t.start()defcall(self):"""循环去获取任务函数并执行任务函数"""current_thread=threading.currentThread()
self.generate_list.append(current_thread)
event=self.q.get()while event !=StopEvent:
func, arguments, callback=eventtry:
result= func(*arguments)
success=TrueexceptException as e:
success=False
result=Noneif callback is notNone:try:
callback(success, result)exceptException as e:passwith self.worker_state(self.free_list, current_thread):ifself.terminal:
event=StopEventelse:
event=self.q.get()else:
self.generate_list.remove(current_thread)defclose(self):"""执行完所有的任务后,所有线程停止"""self.cancel=True
full_size=len(self.generate_list)whilefull_size:
self.q.put(StopEvent)
full_size-= 1
defterminate(self):"""无论是否还有任务,终止线程"""self.terminal=Truewhileself.generate_list:
self.q.put(StopEvent)
self.q.queue.clear()
@contextlib.contextmanagerdefworker_state(self, state_list, worker_thread):"""用于记录线程中正在等待的线程数"""state_list.append(worker_thread)try:yield
finally:
state_list.remove(worker_thread)#How to use
pool= ThreadPool(5)defcallback(status, result):#status, execute action status
#result, execute action return value
pass
defaction(i):print(i)for i in range(10):
ret=pool.run(action, (i,), callback)
time.sleep(5)print(len(pool.generate_list), len(pool.free_list))print(len(pool.generate_list), len(pool.free_list))
pool.close()#pool.terminate()