【python】threadpool线程池模块基本用法
2017-09-15 本文已影响2826人
lndyzwdxhs
安装:
pip install threadpool
0x01 代码剖析
创建线程池,线程数为10:
pool = threadpool.ThreadPool(10)
创建线程请求,包涵调用的函数、参数和回调函数:
requests = threadpool.makeRequests(func, args_list, call_back)
# 源代码
# `args_list`` should be either a 2-item tuple of the list of positional arguments and a dictionary of keyword arguments or a single, non-tuple argument.
args_list必须是包含2个元素的元组,第一个是list,第二个是dict,如果线程函数需要多个参数,需要拼接list或者dict。
# 方法1
lst_vars_1 = ['1', '2', '3']
lst_vars_2 = ['4', '5', '6']
func_var = [(lst_vars_1, None), (lst_vars_2, None)]
# 方法2
dict_vars_1 = {'m':'1', 'n':'2', 'o':'3'}
dict_vars_2 = {'m':'4', 'n':'5', 'o':'6'}
func_var = [(None, dict_vars_1), (None, dict_vars_2)]
将所有要运行多线程的请求扔进线程池:
[pool.putRequest(req) for req in requests]
# 等同于
for req in requests:
pool.putRequest(req)
等待所有的线程完成工作后退出:
pool.wait()
示例:
#!/usr/bin/env python
# coding:utf-8
import time
import random
import threadpool
HEHE = dict()
def sayhello(name, v):
global HEHE
if HEHE.has_key(name):
HEHE[name] = HEHE[name] + '+' + v
else:
HEHE[name] = v
#time.sleep(2)
#name_list = [(['caoshuai', '1'], None), (['yangliu', '2'], None),(['caoshuai', '3'], None),(['ss', '10'], None),(['wwwwww', '12'], None),]
name_list = [(['caoshuai','1'],None),(['caoshuai','2'],None),(['a','3'],None),(['ss','10'],None),(['wwwwww','12'],None),(['m','12'],None),(['n','12'],None),(['b','12'],None),(['v','12'],None),(['x','12'],None),(['z','12'],None),]
#name_list = [1, -5, 6, -4]
start_time = time.time()
pool_t = threadpool.ThreadPool(4)
requestss = threadpool.makeRequests(sayhello, name_list)
[pool_t.putRequest(req) for req in requestss]
pool_t.wait()
print HEHE
print "%s second" % (time.time()-start_time)
while True:
time.sleep(1)
欢迎关注微信公众号(coder0x00)或扫描下方二维码关注,我们将持续搜寻程序员必备基础技能包提供给大家。