您的位置:首页 > 编程语言 > Python开发

python多线程模块threadpool简单使用

2012-05-12 22:54 393 查看
python实现线程池通常使用threading或thread模块来编写,现在已经有了threadpool模块来实现线程池。

英文文档见:http://www.chrisarndt.de/projects/threadpool/

中文文档见:http://gashero.yeax.com/?p=44

现给出一个简易的使用threadpool模块来实现线程池的例子:

#!/usr/bin/env python
import threadpool
import time,random

def hello(str):
time.sleep(2)
return str

def print_result(request, result):
print "the result is %s %r" % (request.requestID, result)

data = [random.randint(1,10) for i in range(20)]

pool = threadpool.ThreadPool(5)
requests = threadpool.makeRequests(hello, data, print_result)
[pool.putRequest(req) for req in requests]
pool.wait()


本文出自 “Get and Share” 博客,请务必保留此出处http://dgfpeak.blog.51cto.com/195468/861994
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: