Python 多处理脚本无限期挂起

标签 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)

最佳答案

问题是,在 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)

关于Python 多处理脚本无限期挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29710851/

相关文章:

python - 如何确定一组元组中的公共(public)元素 - Python

python - 为什么时区感知日期时间的 tzinfo 不等于时区?

c++ - 如何快速创建一堆覆盖两个图像的图像?

Android - 带有图像的 ListView 滚动不流畅

python - 如何添加到字典值或如果不存在则创建

python - 在上采样数据帧时拆分持续时间

html - 图像不是 100% 宽度,并且在我缩小时不能正确扩展

linux - 如何在程序之间创建互斥锁,就像进程之间的互斥锁一样

python multiprocessing只会启动第一个进程

python - 具有多处理的共享内存字符串数组