多处理池方法挂起但处理方法正常

时间:2016-07-17 04:50:12

标签: windows python-2.7 multiprocessing python-multiprocessing pool

我正在尝试使用multiprocessing.Pool方法。它启动并显示所有核心都处于活动状态,但最终只是停止而没有完成。

要解决这个问题,我首先尝试使用一个简单的for循环,并且有效(下面的方法1)。我也尝试使用multiprocessing.Process方法,它也有效(下面的方法2)。

然而,

方法3(下面)只是挂起。在实施multiprocessing.Pool方法时我可能会出错?

import arcpy, os, glob, multiprocessing

# Method 1 works
inws = r'C:\temp\raster_data'
rasters = glob.glob(os.path.join(inws, "*.tif"))

def worker(raster):
    arcpy.arcpy.BuildRasterAttributeTable_management(raster)
    arcpy.AddField_management(raster, "Cover", "TEXT", "", "", 20)

for raster in rasters:
    worker(raster)

# Method 2 works
def mp_worker(raster):
    arcpy.arcpy.BuildRasterAttributeTable_management(raster)
    arcpy.AddField_management(raster, "Cover", "TEXT", "", "", 20)

if __name__ == '__main__':
    inws = r'C:\temp\raster_data'
    rasters = glob.glob(os.path.join(inws, "*.tif"))
    for raster in rasters:
        p = multiprocessing.Process(target=mp_worker(raster))
        p.start()
        p.join()

# Method 3 not working
def mp_worker(raster):
    arcpy.arcpy.BuildRasterAttributeTable_management(raster)
    arcpy.AddField_management(raster, "Cover", "TEXT", "", "", 20)

def mp_handler():
    p = multiprocessing.Pool(8)
    p.map(mp_worker, rasters)

if __name__ == '__main__':
    inws = r'C:\temp\raster_data'
    rasters = glob.glob(os.path.join(inws, "*.tif"))
    mp_handler() 

1 个答案:

答案 0 :(得分:0)

这一行

p = multiprocessing.Process(target=mp_worker, args=(raster,))

应该是这样的

mp_worker

否则,当应该引用函数时,您正在调用函数Simple TCP Packet = TCP Headers | TCP Data TCP Data = Protocol Headers | Protocol Data | Client Hello