python - Multiprocessing pool.map 不会从函数调用中返回

标签 python numpy multiprocessing python-multiprocessing

我有一个简短的片段,使 pool.map 调用挂断。它不会在任何合理的时间内返回。这个想法是拥有几个点列表并找到与另一个点列表对应的度量。

    import numpy as np
    import multiprocessing
    import scipy.spatial as ss

    def dist(args): # returns the sum of distances of the nearest neighbors.
        kdSet, points = args
        dist, _ = kdSet.query(points)
        return np.sum(dist)

    #just define some points to play with
    points = np.array([[13.27, 25.49], [13.18, 25.39], [13.08, 25.39], 
                       [12.99, 25.39], [12.89, 25.39], [12.80, 25.39],
                       [12.71, 25.39], [12.61, 25.30], [12.52, 25.30]]) 
    pointList = [points + idx for idx in range(3)] #creates a list of points

    #create a list of KDTrees for fast lookup. Add some number to make them a little different from pointList
    kdTreeList = [ss.KDTree(pointsLocal + idx/10) for idx, pointsLocal in enumerate(pointList)] 

    #this part works fine: serial execution
    distances = list(map(dist, zip(kdTreeList, pointList)))
    print(distances)

    #this part hangs, if called as multiprocess, expected the same result as above
    with multiprocessing.Pool() as pool:
        print('Calling pool.map')
        distancesMultiprocess = pool.map(dist, zip(kdTreeList, pointList)) #<-This call does not return, but uses the CPU 100%
    print(distancesMultiprocess)

按顺序调用该函数工作正常。但如果在 pool.map 函数中调用,它既不会给出错误,也不返回任何内容。它就卡在那里。 CPU 负载达到 100%,因此发生了一些事情。

出于测试目的删除 KDTree 类并没有显示不同的结果。

由于没有抛出错误,所以我不知道这里出了什么问题。有什么提示吗?

最佳答案

非常感谢 juanpa.arrivilillaga 添加

    if __name__ == "__main__":

成功了。

关于python - Multiprocessing pool.map 不会从函数调用中返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60226273/

相关文章:

python - Odoo 13 - 搜索方法 - 使用 'order' 属性作为模型的相关字段

python - 无法让 PIL 在 Ubuntu 12.04 上正确安装

python - 根据排序值匹配两个数组的元素(Python)

python - 保存许多不同长度的数组

python - 使用 Python 多处理以固定速率安排任务

python - 在同一对象上多处理独立函数的最有效方法

python - 整个数据帧中 FOR 循环中的 IF 语句 : performance improvement

python - pee wee Meta 子类继承

python - 为什么某些数据框数学函数花费更多时间?如何加快它们的速度?

python - 我可以在 Future 中使用 ProcessPoolExecutor 吗?