我正在研究将多处理集成到一些数字图像处理工作流中。以下脚本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。在相关问题 更多 >
编程相关推荐