Python多处理脚本无限期挂起

时间:2015-04-17 23:06:16

标签: python image multiprocessing

我正致力于将多处理功能集成到一些数字图像处理工作流程中。以下脚本1)将图像带转换为numpy数组,2)计算归一化差异植被索引(NDVI)和3)将numpy数组转换回栅格并写入磁盘。该脚本旨在通过多处理了解速度提升。第一部分通过迭代工作区,对每个栅格执行处理以及写入磁盘(总时间= 2分钟)来正常工作。第二个多处理部分"挂起"无限期地产生没有输出。我在哪里遇到脚本的多处理部分?

import arcpy, os, time
from multiprocessing import Pool

arcpy.env.workspace = r'C:\temp\tiles' # Contains 10 images
outws = r'C:\temp\out'

start = time.time()
rasters = arcpy.ListRasters()

for ras in rasters:
    # Calculate NDVI
    red = arcpy.RasterToNumPyArray(os.path.join(ras + "/" + "Band_1"))
    nir = arcpy.RasterToNumPyArray(os.path.join(ras + "/" + "Band_4"))
    ndvi = nir - red / nir + red

    # Convert array to raster
    myRaster = arcpy.NumPyArrayToRaster(ndvi,x_cell_size=0.5)
    myRaster.save(os.path.join(outws, "ndvi_" + ras))

end = time.time()

print "%s sec" % (end-start)

#######################################################
start = time.time()
rasters = arcpy.ListRasters()

def process_img(ras):
    outws = r'C:\temp\out2'
    # Calculate NDVI
    red = arcpy.RasterToNumPyArray(os.path.join(ras + "/" + "Band_1"))
    nir = arcpy.RasterToNumPyArray(os.path.join(ras + "/" + "Band_4"))
    ndvi = nir - red / nir + red

    # Convert array to raster
    myRaster = arcpy.NumPyArrayToRaster(ndvi,x_cell_size=0.5)
    myRaster.save(os.path.join(outws, "ndvi_" + ras))

pool = Pool(processes=4)
pool.map(process_img, rasters)

end = time.time()

print "%s sec" % (end-start)

1 个答案:

答案 0 :(得分:3)

问题是,在Windows上,多处理会重新加载子进程中的脚本,并导致所有顶级代码再次运行...将进程生成到无穷大(或完全挂起)。将所有脚本代码移动到if __name__=="__main__":子句中。有关详细信息,请参阅Programming Guide for Windows

import arcpy, os, time
from multiprocessing import Pool

def process_img(ras):
    outws = r'C:\temp\out2'
    # Calculate NDVI
    red = arcpy.RasterToNumPyArray(os.path.join(ras + "/" + "Band_1"))
    nir = arcpy.RasterToNumPyArray(os.path.join(ras + "/" + "Band_4"))
    ndvi = nir - red / nir + red

    # Convert array to raster
    myRaster = arcpy.NumPyArrayToRaster(ndvi,x_cell_size=0.5)
    myRaster.save(os.path.join(outws, "ndvi_" + ras))

if __name__=="__main__":
    arcpy.env.workspace = r'C:\temp\tiles' # Contains 10 images
    outws = r'C:\temp\out'

    start = time.time()
    rasters = arcpy.ListRasters()

    for ras in rasters:
        # Calculate NDVI
        red = arcpy.RasterToNumPyArray(os.path.join(ras + "/" + "Band_1"))
        nir = arcpy.RasterToNumPyArray(os.path.join(ras + "/" + "Band_4"))
        ndvi = nir - red / nir + red

        # Convert array to raster
        myRaster = arcpy.NumPyArrayToRaster(ndvi,x_cell_size=0.5)
        myRaster.save(os.path.join(outws, "ndvi_" + ras))

    end = time.time()

    print "%s sec" % (end-start)

    #######################################################
    start = time.time()
    rasters = arcpy.ListRasters()


    pool = Pool(processes=4)
    pool.map(process_img, rasters)

    end = time.time()

    print "%s sec" % (end-start)