1 #!/usr/bin/env python2 #-*-coding:utf-8-*-3 #!/usr/bin/env python4 #-*-coding:utf-8-*-5 6 ImportQueue7 ImportThreading8 ImportContextlib9 Import TimeTen OneStopevent =object () A - - classThreadPool (object): the - def __init__(self, max_num): -SELF.Q = queue. Queue ()#queue to hold tasks -Self.max_num = Max_num#maximum number of thread concurrency + -Self.terminal = False#If all threads are terminated for true, new tasks are no longer acquired +Self.generate_list = []#threads that have been created ASelf.free_list = []#Idle Threads at - defRun (self, func, args, callback=None): - """ - thread pool performs a task - :p Aram Func: Task function - :p Aram args: Required parameters for task functions in :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) - : Return: Returns True if the thread pool has terminated otherwise none to """ + - ifLen (self.free_list) = = 0 andLen (self.generate_list) < self.max_num:#no idle threads and no more than the maximum number of threads theSelf.generate_thread ()#Creating Threads *W = (func, args, callback,)#save parameters as tuples $Self.q.put (W)#Add to Task queuePanax Notoginseng - defGenerate_thread (self): the """ + Create a thread A """ thet = Threading. Thread (target=Self.call) + T.start () - $ defCall (self): $ """ - loop to get the task function and execute the task function - """ theCurrent_thread = Threading.currentthread#gets the current thread object -Self.generate_list.append (Current_thread)#add to created line threadWuyi theevent = Self.q.get ()#Get Task - whileEvent! = Stopevent:#if the signal is not stopped Wu -Func, arguments, callback = Event#the values are respectively taken, About Try: $result = Func (*arguments)#Run the function and assign the results to result -Status = True#whether the running result is normal - exceptException as E: -Status = False#not normal Aresult = E#The result is an error message + the ifCallback is notNone:#whether there is a callback function - Try: $Callback (status, result)#Execute callback function the exceptException as E: the Pass the the ifSelf.terminal:#The default is False if the terminal method is called -event = Stopevent#Stop Signal in Else: the #self.free_list.append (Current_thread) #执行完毕任务, add to Idle list the #event = Self.q.get () #获取任务 About #Self.free_list.remove (current_thread) #获取到任务之后, remove from idle the With self.worker_state (self.free_list,current_thread): theevent =Self.q.get () the + - Else: theSelf.generate_list.remove (Current_thread)#If a stop signal is received, it is removed from the created listBayi the defClose (self):#terminating a thread thenum = Len (self.generate_list)#get a thread that has always been created - whileNum: -Self.q.put (stopevent)#Adding a stop signal, several threads add several thenum-= 1 the the #terminating a thread (emptying a queue) the defTerminate (self): - theSelf.terminal = True#change to True, the the whileSelf.generate_list:#If there are created threads surviving94Self.q.put (stopevent)#a few of them send a few signals . theSelf.q.empty ()#Empty Queue the @contextlib. ContextManager the defworker_state (self,free_list,current_thread):98 free_list.append (Current_thread) About Try: - yield101 finally:102 Free_list.remove (Current_thread)103 Import Time104 the defWork (i):106 Print(i)107 108Pool = ThreadPool (10)109 forIteminchRange (50): thePool.run (Func=work, args=(item,))111 #pool.terminate () thePool.close ()
Python Custom thread pool