Python之创建low版的线程池

#!/user/bin/evn python
# -*- coding:utf-8 -*-
import threading,time
import queue
#创建线程池类
class ThreadPool(object):
    def __init__(self,max_num=20):
        #创建队列
        self.queue=queue.Queue(max_num)
        for i in range(max_num):
            #往队列里面依次放入20个线程类名(threading.Thread)
            self.queue.put(threading.Thread)
    
     #获取线程的方法       
    def get_thread(self):
        #从队列里面依次取出线程名
        return self.queue.get()
    #添加线程名到队列里面
    def add_thread(self):
        self.queue.put(threading.Thread)

def func(p,i):
    time.sleep(1)
    print(i)
    p.add_thread()

#创建线程池对象
p=ThreadPool()

for i in range(100):
    ret=p.get_thread()#获取线程类名
    t=ret(target=func,args=(p,i,))#创建线程对象
    t.start()#线程开始执行
原文地址:https://www.cnblogs.com/wangbinbin/p/7475788.html