2019独角兽企业重金招聘Python工程师标准>>>
Python的threadpool模块
这是一个使用python实现的线程池库。
安装
pip install threadpool
文档
http://gashero.yeax.com/?p=44
http://www.chrisarndt.de/projects/threadpool/
测试
使用一个20个线程的线程池进行测试
import threadpool
import time,random
import Queuedef hello1(str): time.sleep(2) return str def print_ret(request, result): print "the result is %s %r\n" % (request.requestID, result) def deal_task(pool):try:pool.poll(True)except Exception, e:print str(e)#lst = [1,2,3,4,5,6,7]
q = Queue.Queue()
for i in range(100):q.put(i)lst = [q.get() for i in range(q.qsize())]pool = threadpool.ThreadPool(20)
requests = threadpool.makeRequests(hello1, lst, print_ret)
for req in requests:pool.putRequest(req)#deal_task(pool)pool.wait()