zoukankan      html  css  js  c++  java
  • 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

    通过观察发现速度并发之后效率大大提高了。

  • 相关阅读:
    Oracle函数如何把符串装换为小写的格式
    Oralce中的synonym同义词
    JS中getYear()的兼容问题
    How to do SSH Tunneling (Port Forwarding)
    所谓深度链接(Deep linking)
    upload size of asp.net
    发一个自动刷网站PV流量的小工具
    解决Visual Studio 2008 下,打开.dbml(LINQ) 文件时,提示"The operation could not be completed." 的问题。
    在资源管理器中使鼠标右键增加一个命令,运行cmd,同时使得当前路径为资源管理器当前的目录
    使用SQL语句获取Sql Server数据库的版本
  • 原文地址:https://www.cnblogs.com/c-x-a/p/9207907.html
Copyright © 2011-2022 走看看