I have the following code for parallel computing of finding shortest path length between nodes in a graph.
For some reason, pool.map() works, but pool.apply() doesn't.
Could you help me trouble shooting what the problem is?
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))
Output:
single core time: 25.18528151512146
pool map 12 core time: 5.043155670166016
pool apply 12 core time: 29.906842470169067