关于python使用threadpool中的函数单个参数和多个参数用法举例
生活随笔
收集整理的這篇文章主要介紹了
关于python使用threadpool中的函数单个参数和多个参数用法举例
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.對單個元素的函數使用線程池:
# encoding:utf-8 __author__='xijun.gong' import threadpooldef 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.gonghi xijunhi gxjun2.對于多個參數的情況使用方式:
''' 遇到問題沒人解答?小編創建了一個Python學習交流QQ群:857662006 尋找有志同道合的小伙伴,互幫互助,群里還有不錯的視頻學習教程和PDF電子書! ''' # encoding:utf-8 __author__='xijun.gong' import threadpooldef 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=133.如果我們想不安參數順序賦值,可以使用這種方式:
# encoding:utf-8 __author__='xijun.gong' import threadpooldef 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總結
以上是生活随笔為你收集整理的关于python使用threadpool中的函数单个参数和多个参数用法举例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python celery多worker
- 下一篇: python实现可以被with上下文管理