python 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() 

如果hello函数有多个参数,那就需要注意下了,这时你就需要使用字典列表

比如如果你有两个参数 ip,port,那么这个data就应该是这种形式:

data=[]

data.append({‘ip’:'***','port':12345})

之前尝试使用下面这种形式,一直报错

data=[]

data.append(('***',12345))

后来仔细看了api文档,终于找到解决办法

以后遇到问题还是好好看看官方API文档来得快啊

posted @ 2013-03-21 11:32  聂风  阅读(2119)  评论(1编辑  收藏  举报