zoukankan      html  css  js  c++  java
  • python | concurrent.futures模块提升数据处理速度

    在默认情况下,Python 程序是单个进程,使用单 CPU 核心执行。致使多核设备无法利用而算力浪费。通过使用 Python 的 concurrent.futures 模块,可以让一个普通的程序转换成适用于多核处理器并行处理的程序,进而提升数据预处理的效率。

    案例:

    简单例子,在单个文件夹中有一个图片数据集,其中有数万张图片。在这里,我们决定使用 1000 张。我们希望在所有图片被传递到深度神经网络之前将其调整为 600×600 像素分辨率的形式。

    标准方法 

    import glob
    import os
    import cv2
    
    ### Loop through all jpg files in the current folder 
    ### Resize each one to size 600x600
    for image_filename in glob.glob("*.jpg"):
     ### Read in the image data
      img = cv2.imread(image_filename) ### Resize the image   img = cv2.resize(img, (600, 600))
    流程:
    1. 首先从需要处理内容的文件(或其他数据)列表开始。
    2. 使用 for 循环逐个处理每个数据,然后在每个循环迭代上运行预处理。

    更快的方法

    流程:
    1.将 jpeg 文件列表分成 4 个小组;
    2.运行 Python 解释器中的 4 个独立实例;
    3.让 Python 的每个实例处理 4 个数据小组中的一个;
    4.结合四个处理过程得到的结果得出最终结果列表。
     
    import glob
    import os
    import cv2
    import concurrent.futures
    
    
    def load_and_resize(image_filename):
    ### Read in the image data
        img = cv2.imread(image_filename)
    
    ### Resize the image
        img = cv2.resize(img, (600, 600)) 
    
    
    ### Create a pool of processes. By default, one is created for each CPU in your machine.
    with concurrent.futures.ProcessPoolExecutor() as executor:
     ### Get a list of files to process
        image_files = glob.glob("*.jpg")
    
     ### Process the list of files, but split the work across the process pool to use all CPUs
     ### Loop through all jpg files in the current folder 
     ### Resize each one to size 600x600
        executor.map(load_and_resize, image_files)

    「executor.map()」将你想要运行的函数和列表作为输入,列表中的每个元素都是我们函数的单个输入。

     但是该方法并不适用于对处理后的结果有特殊顺序要求。
     

  • 相关阅读:
    Spring Boot (20) 拦截器
    Spring Boot (19) servlet、filter、listener
    Spring Boot (18) @Async异步
    Spring Boot (17) 发送邮件
    Spring Boot (16) logback和access日志
    Spring Boot (15) pom.xml设置
    Spring Boot (14) 数据源配置原理
    Spring Boot (13) druid监控
    Spring boot (12) tomcat jdbc连接池
    Spring Boot (11) mybatis 关联映射
  • 原文地址:https://www.cnblogs.com/geo-will/p/9757016.html
Copyright © 2011-2022 走看看