关于python线程池threadpool
#coding=utf-8
import time import threadpool def wait_time(n): print('%d\n' % n) time.sleep(2) #在线程池中开启3个线程,一般小于cpu核数 pool = threadpool.ThreadPool(3) tasks = threadpool.makeRequests(wait_time, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) print(len(tasks)) [pool.putRequest(task) for task in tasks] pool.wait()
''' #结果就会3个一组跑起来 >>> 10 1 2 3 4 5 6 7 8 9 10 >>> '''
#下面这段也可以,把参数提取出来
ns=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
pool1=threadpool.ThreadPool(3)
tasks1=threadpool.makeRequests(long_op,ns)
[pool1.putRequest(t)for t in tasks1]
pool1.wait()