我有一个简短的片段,使 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/