Thread pool:
Custom thread Pool One:
#!/usr/bin/env python#-*-coding:utf-8-*-ImportQueueImportThreadingclassThreadPool (object):def __init__(Self, max_num=20): Self.queue=queue.queue (max_num) forIinchxrange (max_num): Self.queue.put (Threading. Thread)defGet_thread (self):returnSelf.queue.get ()defAdd_thread (self): Self.queue.put (Threading. Thread)"""pool = ThreadPool (+) def func (ARG, p): Print arg import time Time.sleep (2) P.add_thread () for I in Xrange (3 0): thread = Pool.get_thread () t = Thread (Target=func, args= (i, Pool)) T.start ()"""
Custom thread Pool Two:
#!/usr/bin/env python#-*-coding:utf-8-*-ImportQueueImportThreadingImportContextlibImporttimestopevent=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= [] defRun (self, func, args, callback=None):"""The thread pool performs a task:p Aram Func: Task function:p Aram args: task function required parameter:p Aram callback: callback function executed after task execution failed or succeeded, callback function There are 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 terminated, returns true otherwise none""" ifSelf.cancel:return ifLen (self.free_list) = = 0 andLen (self.generate_list) <Self.max_num:self.generate_thread () W=(func, args, callback,) Self.q.put (W)defGenerate_thread (self):"""Create a thread"""T= Threading. Thread (target=self.call) T.start ()defCall (self):"""loop to get the task function and execute the task function"""Current_thread=Threading.currentthread () self.generate_list.append (current_thread) event=Self.q.get () whileEvent! =Stopevent:func, arguments, callback=EventTry: Result= Func (*arguments) Success=TrueexceptException as E:success=False Result=NoneifCallback 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):"""after all the tasks have been performed, all threads stop"""Self.cancel=True full_size=Len (self.generate_list) whilefull_size:self.q.put (stopevent) full_size-= 1defTerminate (self):"""terminate thread, whether or not there is a task"""self.terminal=True whileself.generate_list:self.q.put (stopevent) self.q.queue.clear () @contextlib. ContextManager defworker_state (self, State_list, worker_thread):"""used to record the number of threads waiting in a thread"""state_list.append (Worker_thread)Try: yield finally: State_list.remove (worker_thread)# How to usePool= ThreadPool (5)defCallback (status, result):#status, execute action status #result, execute action return value PassdefAction (i):Print(i) forIinchRange (30): 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 ()
Custom thread pool