依然饭跑跑 发表于 2015-4-26 02:01:55

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 =
pool = threadpool.ThreadPool(5)
requests = threadpool.makeRequests(hello, data, print_result)

pool.wait()
  如果hello函数有多个参数,那就需要注意下了,这时你就需要使用字典列表
  比如如果你有两个参数 ip,port,那么这个data就应该是这种形式:
  data=[]
  data.append({‘ip’:'***','port':12345})
  之前尝试使用下面这种形式,一直报错
  data=[]
  data.append(('***',12345))
  后来仔细看了api文档,终于找到解决办法
  以后遇到问题还是好好看看官方API文档来得快啊
页: [1]
查看完整版本: python threadpool使用注意事项