python 10行代码开启线程池,真香

需要安装 threadpool 线程池第三方库

pip install threadpool

10行代码开启线程池,美汁汁

 

# -*-coding:utf-8-*-
import time
import threadpool
pool=threadpool.ThreadPool(10) # 1.新建线程池
def say(x):
    print('task',x,len(pool.workers))
    time.sleep(1)
lis=[{'msg':x} for x in range(100)]
[pool.putRequest(x) for x in threadpool.makeRequests(say,lis)]
pool.wait()

 

 

搜索