我想在 python 中并行构建一个完全连接的图,并获得一个边缘值列表,例如:以字典格式存储:( node1, node2 ) = edge_value
{ ( node1, node2 ) : edge_value [, ... [, ... ] ] }
为此,我必须首先初始化两个global
变量,G
用于图形和f_correlation
所述字典
import networkx as nx
from multiprocessing import Pool
G = nx.Graph()
f_correlation = {}
然后创建一个函数来构造图形并将其存储到
字典中:( node1, node2 ) = edge_value
f_correlation
def construct_graph_parallelly(pair_with_df):
global G
global f_correlation
pair, df = pair_with_df
i, j = pair
# calculate the edge value and store it in the global variable f_correlation
f_correlation[ (i, j) ] = calculate_value(df, i, j) # this function calculate some value on the dataframe
# here i, j are node in the graph
G.add_edge(i, j, weight = f_correlation[ (i, j) ])
return f_correlation
然后multiprocessing.Pool()
创建一个-instance并调用它的.map()
-method,让代码同时执行:
def make_all_pair_with_df(node_list, df):
all_pair_with_df = []
for i in node_list:
for j in node_list:
if i != j :
pair_with_df = (i,j),df
all_pair_with_df.append(pair_with_df)
return all_pair_with_df
node_list = ['a', 'b', 'c', 'd', 'e']
pool = Pool()
all_pair_with_df = make_all_pair_with_df(node_list, df)
f_correlation = pool.map(construct_graph_parallelly, all_pair_with_df)
pool.close()
print("DONE")
但是当我运行它无限运行的代码时,从不打印“DONE”
问题之一可能是global
-variable 问题,在Globals variables and Python multiprocessing中讨论
但是对于我的工作,我需要全局更新字典和Connected Graph 。
我该怎么做或者我应该做哪些修改才能使它工作?