我正在致力于将多重处理集成到一些数字图像处理工作流程中。以下脚本 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/