首页 > 解决方案 > multiprocessing.Pool.map() 有效,但 Pool.apply() 无效

问题描述

我有以下代码用于并行计算查找图中节点之间的最短路径长度。

出于某种原因, pool.map() 有效,但 pool.apply() 无效。

你能帮我解决问题吗?

import multiprocessing as mp
import time
import networkx as nx


G = nx.fast_gnp_random_graph(2000, 0.001)

# Single core
current_time = time.time()
lengths = [nx.single_source_shortest_path_length(G, node) for node in G.nodes]
time_consumed = time.time() - current_time
print('single core time: %s' % time_consumed)


# pool map

def spl(node):
    return nx.single_source_shortest_path_length(G, node)

pool = mp.Pool(mp.cpu_count())
current_time = time.time()
results = pool.map(spl, G.nodes)
time_consumed = time.time() - current_time
pool.close()
print('pool map %s core time: %s' % (mp.cpu_count(), time_consumed))


# Pool apply
pool = mp.Pool(mp.cpu_count())
current_time = time.time()
results = [pool.apply(spl, args=(node,)) for node in G.nodes]
time_consumed = time.time() - current_time
pool.close()
print('pool apply %s core time: %s' % (mp.cpu_count(), time_consumed))

输出:

single core time: 25.18528151512146
pool map 12 core time: 5.043155670166016
pool apply 12 core time: 29.906842470169067

标签: pythonpython-multiprocessing

解决方案


推荐阅读