关于python使用threadpool中的函数单个参数和多个参数用法举例
1.对单个元素的函数使用线程池:
# encoding:utf-8
__author__='xijun.gong'
import threadpool
def func(name):
print 'hi {}\n'.format(name)
if __name__ == '__main__':
data = ['xijun.gong', 'xijun', 'gxjun']
pool = threadpool.ThreadPool(5)
reqs = threadpool.makeRequests(func, data)
[pool.putRequest(req) for req in reqs]
pool.wait()
结果:
hi xijun.gong
hi xijun
hi gxjun
2.对于多个参数的情况使用方式:
'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:857662006
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
# encoding:utf-8
__author__='xijun.gong'
import threadpool
def func(name):
print 'hi {}\n'.format(name)
def add(a,b):
print '{0}+{1}={2}'.format(a,b,(a+b))
if __name__ == '__main__':
data = [((index,i),None) for index,i in enumerate(range(1,10,2))]#(index,i)也可以写成[index,i]
pool = threadpool.ThreadPool(5)
reqs = threadpool.makeRequests(add, data)
[pool.putRequest(req) for req in reqs]
pool.wait()
结果:
0+1=1
1+3=4
3+7=10
2+5=7
4+9=13
3.如果我们想不安参数顺序赋值,可以使用这种方式:
# encoding:utf-8
__author__='xijun.gong'
import threadpool
def func(name):
print 'hi {}\n'.format(name)
def add(a,b):
print '{0}+{1}={2}'.format(a,b,(a+b))
if __name__ == '__main__':
data = [(None,{'b':index,'a':i}) for index,i in enumerate(range(1,10,2))]
pool = threadpool.ThreadPool(5)
reqs = threadpool.makeRequests(add, data)
[pool.putRequest(req) for req in reqs]
pool.wait()
结果:
1+0=1
3+1=4
5+2=7
7+3=10
9+4=13