我有一个功能
def dist_to_center(ra_center,dec_center):
# finding theta
cos_ra = np.cos(ra_center-var1['ra'])
cos_dec = np.cos(dec_center-var1['dec'])
sin_dec = np.sin(dec_center)*np.sin(var1['dec'])
theta = np.arccos((cos_ra*cos_dec)+sin_dec*(1-cos_ra))
numerator = theta*comoving_dist
denominator = 1+var1['zcosmo']
# THE FINAL CALCULATED DISTANCE TO CENTRE
dist_to_center = (numerator/denominator)
return dist_to_center
我想使用我的处理器,所以我使用multiprocess pool
这样:
if __name__ == '__main__':
pool = Pool(processes=6)
pool.map(dist_to_center, ra_center, dec_center) #calling the function with it's inputs
pool.close()
pool.join()
代码似乎正常并且正在运行,但只运行了1个处理器而不是我调用的6个。我在这里做错了什么?
答案 0 :(得分:1)
您正在将一对一维数组传递给池。您需要自己对数组进行切片,以使Pool了解如何有效地处理它们。例如:
def dist_to_center_mapper(arrays):
return dist_to_center(arrays[0], arrays[1])
ra = np.split(ra_center, 6)
dec = np.split(dec_center, 6)
pool = Pool(processes=6)
pool.map(dist_to_center_mapper, zip(ra, dec))
我认为" mapper"函数是必需的,因为Pool.map()
只接受一个可迭代的参数。因此,我们将两个数组切片列表压缩在一起,以便它们一起发送到多个进程。请注意,如果您需要,可以将数组拆分为多个部分,如果某些部分可能需要不同的时间等。