线程池可以自动分配线程。pip install threadpool
例子:
import requests,threading,hashlib,time #alt+回车自动导入 import threadpool #线程池 li=[] #子线程中,无法获取函数return的返回值,如果需要拿到返回值,可以定义一个空list,把结果放在list中 def down_load_file(url): r=requests.get(url) m=hashlib.md5(url.encode()) print('开始下载%s' % m.hexdigest()) with open('%s.jpg'%m.hexdigest(),'wb') as fw: fw.write(r.content) li.append(m.hexdigest()) url_list = ['http://www.nnzhp.cn/wp-content/uploads/2019/02/jiami.jpeg', 'http://www.nnzhp.cn/wp-content/uploads/2019/03/js.png', 'http://www.nnzhp.cn/wp-content/uploads/2018/08/ab389f04cb5b57344ef9655428bccaec.png' ] pool=threadpool.ThreadPool(10) #创建一个线程池,指定线程池最大启动多少个线程数 reqs=threadpool,makeRequests(down_load_file,url_list) ##生成启动线程用的参数 #makeRequests中只有两个参数,一个是方法名,一个是参数名。如果原方法中要传多个参数,那再定义值得时候要用多维数组 for req in reqes: pool.putRequest(req) pool.wait()#等待子线程运行结束 print(li) print('测试结束')
二、线程池threading.pool的基本用法
安装:
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)
作者:lndyzwdxhs
链接:https://www.jianshu.com/p/adf926412de0
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。