1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 4 ImportQueue5 ImportThreading6 ImportContextlib7 Import Time8 9Stopevent =object ()Ten One A classThreadPool (object): - - def __init__(Self, max_num, max_task_num =None): the 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 ASelf.generate_list = [] atSelf.free_list = [] - - defRun (self, func, args, callback=None): - """ - thread pool performs a task - :p Aram Func: Task function in :p Aram args: Required parameters for task functions - :p Aram Callback: A callback function executed after a failure or success of a task, the callback function has two parameter 1, the task function execution state, 2, the task function return value (default is None, that is: Do not execute the callback function) to : Return: Returns True if the thread pool has terminated otherwise none + """ - ifSelf.cancel: the return * ifLen (self.free_list) = = 0 andLen (self.generate_list) <Self.max_num: $ Self.generate_thread ()Panax NotoginsengW =(func, args, callback,) - Self.q.put (W) the + defGenerate_thread (self): A """ the Create a thread + """ -t = Threading. Thread (target=Self.call) $ T.start () $ - defCall (self): - """ the loop to get the task function and execute the task function - """WuyiCurrent_thread =Threading.currentthread () the self.generate_list.append (Current_thread) - Wuevent =Self.q.get () - whileEvent! =stopevent: About $Func, arguments, callback =Event - Try: -result = Func (*arguments) -Success =True A exceptException as E: +Success =False theresult =None - $ ifCallback is notNone: the Try: the Callback (success, result) the exceptException as E: the Pass - in with Self.worker_state (Self.free_list, current_thread): the ifself.terminal: theevent =stopevent About Else: theevent =Self.q.get () the Else: the + Self.generate_list.remove (Current_thread) - the defClose (self):Bayi """ the after all the tasks have been performed, all threads stop the """ -Self.cancel =True -Full_size =Len (self.generate_list) the whilefull_size: the self.q.put (stopevent) theFull_size-= 1 the - defTerminate (self): the """ the terminate thread, whether or not there is a task the """94Self.terminal =True the the whileself.generate_list: the self.q.put (stopevent)98 About self.q.queue.clear () - 101 @contextlib. ContextManager102 defworker_state (self, State_list, worker_thread):103 """104 used to record the number of threads waiting in a thread the """106 state_list.append (Worker_thread)107 Try:108 yield109 finally: the State_list.remove (Worker_thread)111 the 113 the # How to use the the 117Pool = ThreadPool (5)118 119 defCallback (status, result): - #status, execute action status121 #result, execute action return value122 Pass123 124 the defAction (i):126 Print(i)127 - forIinchRange (30):129RET =Pool.run (Action, (I,), callback) the 131Time.sleep (5) the Print(Len (pool.generate_list), Len (pool.free_list))133 Print(Len (pool.generate_list), Len (pool.free_list))134 #pool.close ()135 #pool.terminate ()
thread pool (excellent)
1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 ImportQueue4 ImportThreading5 6 7 classThreadPool (object):8 9 def __init__(Self, max_num=20):TenSelf.queue =queue.queue (max_num) One forIinchxrange (max_num): A Self.queue.put (Threading. Thread) - - defGet_thread (self): the returnSelf.queue.get () - - defAdd_thread (self): - Self.queue.put (Threading. Thread) + - """ + pool = ThreadPool (Ten) A at def func (ARG, p): - Print arg - Import Time - Time.sleep (2) - P.add_thread () - in - For I in xrange (+): to thread = Pool.get_thread () + t = thread (Target=func, args= (i, pool)) - T.start () the """
Reproduced
Wu Jianzi
Source: http://www.cnblogs.com/wupeiqi/
Two implementations of the python-thread pool "reprint"