# 放線程的一個池子
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])