亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python多線程方法詳解

 更新時間:2022年01月18日 09:28:48   作者:python_rser  
大家好,本篇文章主要講的是python多線程方法詳解,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下

處理多個數(shù)據(jù)和多文件時,使用for循環(huán)的速度非常慢,此時需要用多線程來加速運行進度,常用的模塊為multiprocess和joblib,下面對兩種包我常用的方法進行說明。

1、模塊安裝

pip install multiprocessing
pip install joblib

2、以分塊計算NDVI為例

首先導入需要的包

import numpy as np
from osgeo import gdal
import time
from multiprocessing import cpu_count
from multiprocessing import Pool
from joblib import Parallel, delayed

定義GdalUtil類,以讀取遙感數(shù)據(jù)

class GdalUtil:
    def __init__(self):
        pass
    @staticmethod
    def read_file(raster_file, read_band=None):
        """讀取柵格數(shù)據(jù)"""
        # 注冊柵格驅動
        gdal.AllRegister()
        gdal.SetConfigOption('gdal_FILENAME_IS_UTF8', 'YES')
        # 打開輸入圖像
        dataset = gdal.Open(raster_file, gdal.GA_ReadOnly)
        if dataset == None:
            print('打開圖像{0} 失敗.\n', raster_file)
        # 列
        raster_width = dataset.RasterXSize
        # 行
        raster_height = dataset.RasterYSize
        # 讀取數(shù)據(jù)
        if read_band == None:
            data_array = dataset.ReadAsArray(0, 0, raster_width, raster_height)
        else:
            band = dataset.GetRasterBand(read_band)
            data_array = band.ReadAsArray(0, 0, raster_width, raster_height)
        return data_array
 
    @staticmethod
    def read_block_data(dataset, band_num, cols_read, rows_read, start_col=0, start_row=0):
        band = dataset.GetRasterBand(band_num)
        res_data = band.ReadAsArray(start_col, start_row, cols_read, rows_read)
        return res_data
 
    @staticmethod
    def get_raster_band(raster_path):
        # 注冊柵格驅動
        gdal.AllRegister()
        gdal.SetConfigOption('gdal_FILENAME_IS_UTF8', 'YES')
        # 打開輸入圖像
        dataset = gdal.Open(raster_path, gdal.GA_ReadOnly)
        if dataset == None:
            print('打開圖像{0} 失敗.\n', raster_path)
        raster_band = dataset.RasterCount
        return raster_band
 
    @staticmethod
    def get_file_size(raster_path):
        """獲取柵格仿射變換參數(shù)"""
        # 注冊柵格驅動
        gdal.AllRegister()
        gdal.SetConfigOption('gdal_FILENAME_IS_UTF8', 'YES')
 
        # 打開輸入圖像
        dataset = gdal.Open(raster_path, gdal.GA_ReadOnly)
        if dataset == None:
            print('打開圖像{0} 失敗.\n', raster_path)
        # 列
        raster_width = dataset.RasterXSize
        # 行
        raster_height = dataset.RasterYSize
        return raster_width, raster_height
 
    @staticmethod
    def get_file_geotransform(raster_path):
        """獲取柵格仿射變換參數(shù)"""
        # 注冊柵格驅動
        gdal.AllRegister()
        gdal.SetConfigOption('gdal_FILENAME_IS_UTF8', 'YES')
 
        # 打開輸入圖像
        dataset = gdal.Open(raster_path, gdal.GA_ReadOnly)
        if dataset == None:
            print('打開圖像{0} 失敗.\n', raster_path)
 
        # 獲取輸入圖像仿射變換參數(shù)
        input_geotransform = dataset.GetGeoTransform()
        return input_geotransform
 
    @staticmethod
    def get_file_proj(raster_path):
        """獲取柵格圖像空間參考"""
        # 注冊柵格驅動
        gdal.AllRegister()
        gdal.SetConfigOption('gdal_FILENAME_IS_UTF8', 'YES')
 
        # 打開輸入圖像
        dataset = gdal.Open(raster_path, gdal.GA_ReadOnly)
        if dataset == None:
            print('打開圖像{0} 失敗.\n', raster_path)
 
        # 獲取輸入圖像空間參考
        input_project = dataset.GetProjection()
        return input_project
 
    @staticmethod
    def write_file(dataset, geotransform, project, output_path, out_format='GTiff', eType=gdal.GDT_Float32):
        """寫入柵格"""
        if np.ndim(dataset) == 3:
            out_band, out_rows, out_cols = dataset.shape
        else:
            out_band = 1
            out_rows, out_cols = dataset.shape
 
        # 創(chuàng)建指定輸出格式的驅動
        out_driver = gdal.GetDriverByName(out_format)
        if out_driver == None:
            print('格式%s 不支持Creat()方法.\n', out_format)
            return
 
        out_dataset = out_driver.Create(output_path, xsize=out_cols,
                                        ysize=out_rows, bands=out_band,
                                        eType=eType)
        # 設置輸出圖像的仿射參數(shù)
        out_dataset.SetGeoTransform(geotransform)
 
        # 設置輸出圖像的投影參數(shù)
        out_dataset.SetProjection(project)
 
        # 寫出數(shù)據(jù)
        if out_band == 1:
            out_dataset.GetRasterBand(1).WriteArray(dataset)
        else:
            for i in range(out_band):
                out_dataset.GetRasterBand(i + 1).WriteArray(dataset[i])
        del out_dataset

定義計算NDVI的函數(shù)

def cal_ndvi(multi):
    '''
    計算高分NDVI
    :param multi:格式為列表,依次包含[遙感文件路徑,開始行號,開始列號,待讀的行數(shù),待讀的列數(shù)]
    :return: NDVI數(shù)組
    '''
    input_file, start_col, start_row, cols_step, rows_step = multi
    dataset = gdal.Open(input_file, gdal.GA_ReadOnly)
    nir_data = GdalUtil.read_block_data(dataset, 4, cols_step, rows_step, start_col=start_col, start_row=start_row)
    red_data = GdalUtil.read_block_data(dataset, 3, cols_step, rows_step, start_col=start_col, start_row=start_row)
    ndvi = (nir_data - red_data) / (nir_data + red_data)
    ndvi[(ndvi > 1.5) | (ndvi < -1)] = 0
    return ndvi
定義主函數(shù)
if __name__ == "__main__":
    input_file = r'D:\originalData\GF1\namucuo2021.tif'
    output_file = r'D:\originalData\GF1\namucuo2021_ndvi.tif'
    method = 'joblib'
    # method = 'multiprocessing'
    # 獲取文件主要信息
    raster_cols, raster_rows = GdalUtil.get_file_size(input_file)
    geotransform = GdalUtil.get_file_geotransform(input_file)
    project = GdalUtil.get_file_proj(input_file)
    # 定義分塊大小
    rows_block_size = 50
    cols_block_size = 50
    multi = []
    for j in range(0, raster_rows, rows_block_size):
        for i in range(0, raster_cols, cols_block_size):
            if j + rows_block_size < raster_rows:
                rows_step = rows_block_size
            else:
                rows_step = raster_rows - j
            # 數(shù)據(jù)橫向步長
            if i + cols_block_size < raster_cols:
                cols_step = cols_block_size
            else:
                cols_step = raster_cols - i
            temp_multi = [input_file, i, j, cols_step, rows_step]
            multi.append(temp_multi)
 
    t1 = time.time()
    if method == 'multiprocessing':
        # multiprocessing方法
        pool = Pool(processes=cpu_count()-1)
        # 注意map函數(shù)中傳入的參數(shù)應該是可迭代對象,如list;返回值為list
        res = pool.map(cal_ndvi, multi)
        pool.close()
        pool.join()
    else:
        # joblib方法
        res = Parallel(n_jobs=-1)(delayed(cal_ndvi)(input_list) for input_list in multi)
 
    t2 = time.time()
    print("Total time:" + (t2 - t1).__str__())
 
    # 將multiprocessing中的結果提取出來,放回對應的矩陣位置中
    out_data = np.zeros([raster_rows, raster_cols], dtype='float')
    for result, input_multi in zip(res, multi):
        start_col = input_multi[1]
        start_row = input_multi[2]
        cols_step = input_multi[3]
        rows_step = input_multi[4]
        out_data[start_row:start_row + rows_step, start_col:start_col + cols_step] = result
 
    GdalUtil.write_file(out_data, geotransform, project, output_file)

雙重for循環(huán)時,兩層for循環(huán)都使用multiprocessing時會報錯,這時可以外層for循環(huán)使用joblib方法,內層for循環(huán)改為multiprocessing方法,不會報錯

到此這篇關于python多線程方法詳解的文章就介紹到這了,更多相關python多線程內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 基于PyQt5實現(xiàn)一個無線網(wǎng)連接器

    基于PyQt5實現(xiàn)一個無線網(wǎng)連接器

    為了方便不會python的朋友也能夠使用,本文將用pyqt5將制作一個帶界面的無線網(wǎng)連接器,文中的示例代碼講解詳細,感興趣的可以了解一下
    2022-08-08
  • Python使用Pillow進行圖像處理

    Python使用Pillow進行圖像處理

    這篇文章介紹了Python使用Pillow進行圖像處理的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • 詳解Python3 pickle模塊用法

    詳解Python3 pickle模塊用法

    pickle模塊中的兩個主要函數(shù)是dump()和load(),這篇文章主要介紹了Python3 pickle模塊用法,需要的朋友可以參考下
    2019-09-09
  • python namedtuple函數(shù)的使用

    python namedtuple函數(shù)的使用

    今天介紹collections里面的一個好用的小函數(shù): namedtuple函數(shù)(不創(chuàng)類而可以擁有類的便利),例如可以使用object.attribute
    2021-06-06
  • Python sort 自定義函數(shù)排序問題

    Python sort 自定義函數(shù)排序問題

    這篇文章主要介紹了Python sort 自定義函數(shù)排序問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Python判斷值是否在list或set中的性能對比分析

    Python判斷值是否在list或set中的性能對比分析

    這篇文章主要介紹了Python判斷值是否在list或set中的性能對比分析,結合實例形式對比分析了使用list與set循環(huán)的執(zhí)行效率,需要的朋友可以參考下
    2016-04-04
  • 如何使用python把ppt轉換成pdf

    如何使用python把ppt轉換成pdf

    這篇文章主要介紹了如何使用python把ppt轉換成pdf,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-06-06
  • 用Python刪除本地目錄下某一時間點之前創(chuàng)建的所有文件的實例

    用Python刪除本地目錄下某一時間點之前創(chuàng)建的所有文件的實例

    下面小編就為大家分享一篇用Python刪除本地目錄下某一時間點之前創(chuàng)建的所有文件的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • python用opencv完成圖像分割并進行目標物的提取

    python用opencv完成圖像分割并進行目標物的提取

    這篇文章主要介紹了python用opencv完成圖像分割并進行目標物的提取,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-05-05
  • python中線程和進程有何區(qū)別

    python中線程和進程有何區(qū)別

    在本篇文章里小編給大家整理的是一篇關于python中線程和進程的區(qū)別相關知識點,有需要的朋友們可以參考下。
    2020-06-06

最新評論