# 放线程的一个池子
import threadpool
def say(num):
print(num)
res = list(range(101))
pool = threadpool.ThreadPool(10) # 建立一个线程池
reqs = threadpool.makeRequests(say, res) # 生成线程要执行的全部线程
# for req in reqs:
# pool.putRequest(req) # 实际才去执行的
[pool.putRequest(req) for req in reqs]
pool.wait() # 等待 其余线程执行结束
封装线程池
import threadpoolclass MyPool(object): def __init__(self, func, size=20, data=None): self.func = func self.size = size self.data = data self.pool() # 实例化以后就能够了 def pool(self): pool = threadpool.ThreadPool(self.size) # 建立一个线程池,指定大小 reqs = threadpool.makeRequests(self.func, self.data) # 生成请求,分配数据 [pool.putRequest(req) for req in reqs] # 执行函数 pool.wait() # 等待线程执行完成def down(num): print(num)my = MyPool(func=down, data=[1, 2, 3, 4, 5, 6, 7, 8, 9])