python并发模块之concurrent.futures(二)
2018-06-21 11:10
369 查看
python并发模块之concurrent.futures(二)
上次我们简单的了解下,模块的一些基本方法和用法,这里我们进一步对concurrent.futures做一个了解和拓展.
上次的内容点这。
python并发模块之concurrent.futures(二)
以下载图片为例子,下面的程序是顺序下载http://www.58pic.com/newpic/28660111.html网站的24个表情 。
from requests_html import HTMLSession
import os
import time
BASE_PATH="downloads"
class Get_Image():
def __init__(self):
self.timeout=20
self.session=HTMLSession()
def getiamge(self,url):
req=self.session.get(url,timeout=self.timeout)
if req.status_code==200:
imgurllist=req.html.xpath("//ul[@class='emoticon-model']/li/img/@data-big")
for index,url in enumerate(imgurllist):
print(f"开始下载第{index+1}张图片")
self.save_image(url,index+1)
else:
print("下载失败")
def save_image(self,imgurl,index):
print(f"当前下载链接:{imgurl}")
buff=self.session.get(imgurl,timeout=self.timeout).content
file_path=os.path.join(os.path.dirname(os.path.abspath(__file__)),BASE_PATH)
if not os.path.exists(file_path):
os.makedirs(file_path)
with open(os.path.join(file_path,f"{index}.png"),"wb") as fs:
fs.write(buff)
if __name__ == '__main__':
start_url="http://www.58pic.com/newpic/28660111.html"
start=time.time()
Get_Image().getiamge(start_url)
end=time.time()
print(f"顺序下载24张图片用时:{end-start}")
#运行了两次结果分别为
#顺序下载24张图片用时:14.926000356674194
#顺序下载24张图片用时:14.07800030708313
使用concurrent.futures修改成并发之后
from requests_html import HTMLSession
import os
import time
from concurrent.futures import ThreadPoolExecutor
BASE_PATH="downloads"
MAX_WORKERS = 10 #最多使用10个线程
class Get_Image():
def __init__(self):
self.timeout=20
self.session=HTMLSession()
def getiamge(self,url):
req=self.session.get(url,timeout=self.timeout)
if req.status_code==200:
imgurllist=req.html.xpath("//ul[@class='emoticon-model']/li/img/@data-big")
works=min(len(imgurllist),MAX_WORKERS)
with ThreadPoolExecutor(works) as excutor:
res=excutor.map(self.save_image,imgurllist,range(1,25))
return len(list(res))
else:
print("下载失败")
def save_image(self,imgurl,index):
print(f"当前下载链接:{imgurl}")
buff=self.session.get(imgurl,timeout=self.timeout).content
file_path=os.path.join(os.path.dirname(os.path.abspath(__file__)),BASE_PATH)
if not os.path.exists(file_path):
os.makedirs(file_path)
with open(os.path.join(file_path,f"{index}.png"),"wb") as fs:
fs.write(buff)
if __name__ == '__main__':
start_url="http://www.58pic.com/newpic/28660111.html"
start=time.time()
Get_Image().getiamge(start_url)
end=time.time()
print(f"并发下载24张图片用时:{end-start}")
#运行了两次结果分别为
#并发下载24张图片用时:7.737000226974487
#并发下载24张图片用时:7.083999872207642
通过观察发现速度并发之后效率大大提高了。
相关文章推荐
- python并发模块之concurrent.futures(一)
- python concurrent.futures并发库 多进程 多线程
- 使用concurrent.futures模块并发,实现进程池、线程池
- Python并发concurrent.futures和asyncio
- python并发处理concurrent.futures
- python并发之concurrent.futures
- python并发库:concurrent.futures的使用
- python并发之concurrent.futures
- 在python中使用concurrent.futures实现进程池和线程池
- 小猪的Python学习之旅 —— 11.Python并发之threading模块(2)
- 利用python并发模块进行网站的状态检测
- python简单粗暴多进程之concurrent.futures
- python异步并发模块concurrent.futures简析
- python concurrent.futures
- 11.python并发入门(part9 多进程模块multiprocessing基本用法)
- Python3之concurrent.futures实现线程池,进程池
- python之多线程并发处理模块-threading
- 线程池,进程池 concurrent.futures 模块介绍
- Python 标准库简介concurrent.futures
- 35、concurrent.futures模块与协程