一、安装与简介
pip install threadpool
pool = ThreadPool(poolsize) requests = makeRequests(some_callable, list_of_args, callback) [pool.putRequest(req) for req in requests] pool.wait()
第一行定义了一个线程池,表示最多可以创建poolsize这么多线程;
第二行是调用makeRequests创建了要开启多线程的函数,以及函数相关参数和回调函数,其中回调函数可以不写,default是无,也就是说makeRequests只需要2个参数就可以运行;
第三行用法比较奇怪,是将所有要运行多线程的请求扔进线程池,[pool.putRequest(req) for req in requests]等同于
for req in requests:
pool.putRequest(req)
第四行是等待所有的线程完成工作后退出。
二、代码实例
1 import time 2 def sayhello(str): 3 print "Hello ",str 4 time.sleep(2) 5 6 name_list =[‘xiaozi‘,‘aa‘,‘bb‘,‘cc‘] 7 start_time = time.time() 8 for i in range(len(name_list)): 9 sayhello(name_list[i]) 10 print ‘%d second‘% (time.time()-start_time)
改用线程池代码,花费时间更少,更效率
1 import time 2 import threadpool 3 def sayhello(str): 4 print "Hello ",str 5 time.sleep(2) 6 7 name_list =[‘xiaozi‘,‘aa‘,‘bb‘,‘cc‘] 8 start_time = time.time() 9 pool = threadpool.ThreadPool(10) 10 requests = threadpool.makeRequests(sayhello, name_list) 11 [pool.putRequest(req) for req in requests] 12 pool.wait() 13 print ‘%d second‘% (time.time()-start_time)
当函数有多个参数的情况,函数调用时第一个解包list,第二个解包dict,所以可以这样:
1 def hello(m, n, o): 2 """""" 3 print "m = %s, n = %s, o = %s"%(m, n, o) 4 5 6 if __name__ == ‘__main__‘: 7 8 # 方法1 9 lst_vars_1 = [‘1‘, ‘2‘, ‘3‘] 10 lst_vars_2 = [‘4‘, ‘5‘, ‘6‘] 11 func_var = [(lst_vars_1, None), (lst_vars_2, None)] 12 # 方法2 13 dict_vars_1 = {‘m‘:‘1‘, ‘n‘:‘2‘, ‘o‘:‘3‘} 14 dict_vars_2 = {‘m‘:‘4‘, ‘n‘:‘5‘, ‘o‘:‘6‘} 15 func_var = [(None, dict_vars_1), (None, dict_vars_2)] 16 17 pool = threadpool.ThreadPool(2) 18 requests = threadpool.makeRequests(hello, func_var) 19 [pool.putRequest(req) for req in requests] 20 pool.wait()
需要把所传入的参数进行转换,然后带人线程池。
1 def getuserdic(): 2 username_list=[‘xiaozi‘,‘administrator‘] 3 password_list=[‘root‘,‘‘,‘abc123!‘,‘123456‘,‘password‘,‘root‘] 4 userlist = [] 5 6 for username in username_list: 7 8 user =username.rstrip() 9 for password in password_list: 10 pwd = password.rstrip() 11 userdic ={} 12 userdic[‘user‘]=user 13 userdic[‘pwd‘] = pwd 14 tmp=(None,userdic) 15 userlist.append(tmp) 16 return userlist
原文:https://www.cnblogs.com/zzmx0/p/12770571.html