python并发模块之concurrent.futures(二)
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
通过观察发现速度并发之后效率大大提高了。

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Spring Boot快速开发利器:Spring Boot CLI
Spring Boot CLI(Command Line Interface)是一个命令行工具,您可以用它来快速构建Spring原型应用。通过Spring Boot CLI,我们可以通过编写Groovy脚本来快速的构建出Spring Boot应用,并通过命令行的方式将其运行起来。下来,我们来学习一下如何安装和使用Spring Boot CLI。 安装Spring Boot CLI 关于Spring Boot CLI的安装方式有很多,这里根据目前主要主流的开发平台,具体介绍一下Windows和Mac下的安装方式: 通用安装 先介绍一个所有平台都可以使用的安装方法。 第一步:下载Spring Boot CLI的工具包: spring-boot-cli-2.0.1.RELEASE-bin.zip spring-boot-cli-2.0.1.RELEA
- 下一篇
python设计模式之单例模式(一)
单例设计模式的概念: 单例设计模式即确保类有且只有一个特定类型的对象,并提供全局访问点。一般我们操作数据库的时候为了避免统一资源产生互相冲突,创建单例模式可以维护数据的唯一性。 单例模式的特性: 确保类有且只有一个对象被创建。 为对象提供一个访问点,以使程序可以全局访问对象。 控制共享资源的并行访问。 下面是单例模式的UML图。(注:UML-Unified Model Language 统一建模语言,又称标准建模语言。是用来对软件密集系统进行可视化建模的一种语) 传统的单例模式的实现方法是,使构造函数私有化,并创建一个静态方法来完成对象的初始化,对象在第一次调用时创建,以后这个类将返回同一个对象. 单例模式的实现 网上找了一个C#的例子(其中考虑到多线程的问题)可以了解下,下面我们主要介绍Python的单例模式的使用。 ///<summary> ///单例模式的实现 ///</summary> publicclassSingleton { //定义一个静态变量来保存类的实例 privatestaticSingletonuniqueInstance; //定义一个...
相关文章
文章评论
共有0条评论来说两句吧...