这是我的问题Paralel for loop, map() works, pool.map() Gives TypeError的后续。我想做multiprocessing.Pool.map(compare_clusters, clusters_1, clusters_2)
,其中 compare_clusters 是一个函数, clusters_1 是对象列表, clusters_2 也是对象列表。该问题的答案清楚地表明,不像map
,multiprocessing.Pool.map
只能采用一个迭代器,在这种情况下clusters_2
必须是块大小。
所以我的问题是,如何用两个迭代器并行化一个循环?
编码
spectra_names, condensed_distance_matrix, index_0 = [], [], 0
for index_1, index_2 in itertools.combinations(range(len(clusters)), 2):
if index_0 == index_1:
index_0 += 1
spectra_names.append(clusters[index_1].get_names()[0])
try:
distance = 1/float(compare_clusters(clusters[index_1], clusters[index_2],maxiter=50))
except:
distance = 10
condensed_distance_matrix.append(distance)
我如何尝试并行化它
from multiprocessing import Pool
condensed_distance_matrix, spectra_names, index_0, clusters_1, clusters_2 = [], [], 0, [], []
for index_1, index_2 in itertools.combinations(range(len(clusters)), 2):
if index_0 == index_1:
index_0 += 1
spectra_names.append(clusters[index_1].get_names()[0])
clusters_1.append(clusters[index_1])
clusters_2.append(clusters[index_2])
pool = Pool()
condensed_distance_matrix_values = pool.map(compare_clusters, clusters_1, clusters_2)
for value in condensed_distance_matrix_values :
try:
distance = 1/float(value)
except:
distance = 10
condensed_distance_matrix.append(distance)