A simple implementation of a thread pool:
Import Queueimport threadingimport timeclass ThreadPool (object): #创建线程池类 def __init__ (self, max_num=20): #创建一个最大长度为20的队列 self.queue = queue. Queue (max_num) #创建一个队列 for i in range (max_num): #循环把线程对象加入到队列中 self.queue.put (threading. Thread) #把线程的类名放进去, execute this queue def get_thread (self): #定义方法从队列里获取线程 return Self.queue.get () #在队列中获取值 def add_thread (self): #线程执行完任务后, add thread Self.queue.put (threading) in the queue . Thread) def func (pool,a1): time.sleep (1) print (A1) pool.add_thread () #线程执行完任务后, plus one thread p = in the queue ThreadPool #执行init方法; executes up to 10 threads at a time for I in range: thread = P.get_thread () #线程池10个线程, Each time the loop takes one to get the class name, no wait for t = thread (Target=func, args= (P, I,)) #创建线程; The thread performs this task of the Func function; args is the parameter passed to the function T.start () #激活线程
Complex thread pool
Thread Pool essentials:
1, when creating a thread pool, you create threads when you need to execute the thread, instead of creating the maximum queue for execution.
2, create a callback function, check out the remaining queue of tasks, when the thread finishes executing the function of the notification thread pool,
3, use the thread pool to cycle through the task and execute
4, thread pool, let its own to activate the thread, after execution completes, closes the exit
Import Queueimport threadingimport Timeimport contextlibstopevent = object () class ThreadPool (object): Def __init__ (self , max_num): self.q = queue. Queue () # Number of threads created (maximum thread pool capacity) Self.max_num = Max_num self.terminal = False #如果为True terminating all threads, not acquiring new tasks SE Lf.generate_list = [] # The list of truly created threads self.free_list = []# Number of idle threads def run (self, func, args, Callback=none): The thread pool performs a task:p Aram Func: Task function:p Aram args: The task function requires parameters:p Aram callback: A callback function that executes after a task fails or succeeds, and the callback function has Two parameters 1, task function execution State, 2, Task function return value (default is None, that is: Do not execute callback function): return: If the thread pool has been terminated, returns true otherwise none "" "If Len (self.free_lis t) = = 0 and len (self.generate_list) < Self.max_num:self.generate_thread () #创建线程 w = (func, args, CA Llback,) #把参数封装成元祖 self.q.put (w) #添加到任务队列 def generate_thread (self): "" Creates a Thread "" " t = Threading. Thread (Target=self.call) T.start () def call (self): "" Loop to get the task function and execute the task function "" "Current_thread = threading.currentthread # Gets the current thread self.generate_list.append (current_thread) #添加 To the line that has been created thread event = Self.q.get () # Takes a task and executes the while event! = stopevent: # is a tuple = "is a task; If you do not perform a task for the stop signal fun C, arguments, callback = Event #解开任务包, remove the value Try:result = func (*arguments) #运行函数, assign the results to result Status = True #运行结果是否正常 except Exception as E:status = False #表示运行不正常 result = e #结果为错误信息 If callback is not None: #是否存在回调函数 Try:callbac K (status, result) #执行回调函数 except Exception as E:pass if self.terminal: # silent False if called terminal method event = Stopevent #等于全局变量, indicates stop signal else: # SELF.FREE_LIST.A Ppend (Current_thread) #执行完毕任务, add to idle List # event = Self.q.get () #获取任务 # Self.free_list.remove (current_thread) # Get toRemoved from the idle list; not a tuple, not a task with self.worker_state (Self.free_list, current_thread): event = Self.q.get () else:self.generate_list.remove (current_thread) #如果收到终止信号, remove def Close from the list of threads that have been created (self ): #终止线程 num = Len (self.generate_list) #获取总共创建的线程数 while Num:self.q.put (stopevent) #添加停止信号, there are multiple Fewer threads add how many signaled signaled num-= 1 def terminate (self): #终止线程 (empty queue) Self.terminal = True #把默认的False更改成True While self.generate_list: #如果有已经创建线程存活 self.q.put (stopevent) #有几个线程就发几个终止信号 self.q.empty () #清空队列 @contextlib. ContextManager def worker_state (self, State_list, worker_thread): State_list.append (Worker_threa d) Try:yield Finally:state_list.remove (worker_thread) def work (i): print (i) pool = ThreadPool for item in range: Pool.run (func=work, args= (item)) # Put the task in the queue # Start Processing Task #-Create Thread # -Have idle threads, select no longer create lines#-cannot be higher than the thread pool limit #-Judging by the number of tasks #-threads go to the queue to fetch tasks pool.terminate ()
This article refer to Oldboy Wu Peiqi teacher (Detailed address:) http://www.cnblogs.com/wupeiqi/articles/4839959.html
Python thread pool Implementation