Class ThreadPool (object): def __init__ (self, max_num=20): # Create a queue with a maximum of 10 data Self.queue = queue. Queue (max_num) # populate thread classes in Queues # "Thread class, Thread class, Thread class, Thread class, Thread class, thread class, thread class" for I in range (max_num): self.queue.put ( Threading. Thread) def get_thread (self): # go to the queue to go to the data, # queue characteristics, if any, to joins that one out # if not, block, return Self.queue.get () def add_thread (self): # Add a thread class Self.queue.put (threading) to the queue . Thread) # pool = ThreadPool (ten) Pool = ThreadPool (+) def func (ARG, p): # function Content print (ARG) import time Time.sleep (2) # Online constructor add a thread back to the thread pool p.add_thread () for I in range: # go to the thread pool and if there is one, take it in the pool, If not, wait until someone returns the thread to the thread pool thread = Pool.get_thread () # thread = Threading. Thread t = Thread (Target=func, args= (i, pool)) T.start ()
Python Simple thread pool