使用多个 inputs/outputs geodataframe-variables 并行化一个函数

Parallelize a function with multiple inputs/outputs geodataframe-variables

使用之前的答案(谢谢 Booboo), 代码思路是:

from multiprocessing import Pool

def worker_1(x, y, z):
    ...
    t = zip(list_of_Polygon,list_of_Point,column_Point)
return t

def collected_result(t):
    x, y, z = t # unpack
    save_shp("polys.shp",x)
    save_shp("point.shp",y,z)

if __name__ == '__main__':

gg = gpd.read_file("name.shp")
pool = Pool()
for index, pol in gg.iterrows():
    xlon ,ylat = gg.centroid
    result = pool.starmap(worker_1, zip(pol,xlon,ylat))
    # or
    # result = mp.Process(worker_1,args = (pol,xlon,ylat))
    pool.close()
    pool.join()  

collected_result(result)

但是地理数据框(多边形、点)不可迭代,所以我不能使用池,有什么并行化建议吗?

如何压缩worker_1中的(geodataframe)输出然后独立保存(或shapefile中的多个图层),最好使用全局参数? ...因为 zip 仅保存列表(对*)?

好吧,如果我明白你想做什么,也许下面就是你需要的。在这里,我正在构建 args 列表,它将通过迭代 gg.iterrows() 用作 iterable 的参数 starmap (不需要使用 zip):

from multiprocessing import Pool

def worker_1(pol, xlon, ylat):
    ...
    t = zip(list_of_Polygon, list_of_Point, column_Point)
    return t

def collected_result(t):
    x, y, z = t # unpack
    save_shp("polys.shp", x)
    save_shp("point.shp", y, z)

if __name__ == '__main__':

    gg = gpd.read_file("name.shp")
    pool = Pool()
    args = []
    for index, pol in gg.iterrows():
        xlon, ylat = gg.centroid
        args.append((pol, xlon, ylat))
    result = pool.starmap(worker_1, args)
    pool.close()
    pool.join()      
    collected_result(result)

您正在创建一个 Pool 实例,并在循环中重复调用方法 starmapclosejoin。但是一旦你在 Pool 实例上调用 close,你就不能再向池中提交任何任务(即再次调用 starmap),所以我认为你的 looping/indentation 完全错了。