我正在将图形对象写入 xml 表示。我的单片代码运行良好,但在我的大图上太慢了。我正在尝试将其并行化,但我没有SubElement
从池中获得支持。我确定我遗漏了一些明显的东西,但我是 python 新手。
import networkx as nx
import lxml.etree as et
from multiprocessing import Pool
G = nx.petersen_graph()
# For any graph, make a node subelement with the id being the node label
def getNodeAttributes(index):
et.SubElement(nodes, "node", attrib={'id': str(G.nodes()[index])})
# Do it with one monolithic process
network = et.Element("network", attrib={"name": "Petersen Graph"})
nodes = et.SubElement(network, "nodes")
for i in range(len(G)):
getNodeAttributes(i)
et.dump(network)
<network name="Petersen Graph">
<nodes>
<node id="0"/>
<node id="1"/>
<node id="2"/>
<node id="3"/>
<node id="4"/>
<node id="5"/>
<node id="6"/>
<node id="7"/>
<node id="8"/>
<node id="9"/>
</nodes>
</network>
# Do it again, but with pool.map in parallel
network = et.Element("network", attrib={"name": "Petersen Graph"})
nodes = et.SubElement(network, "nodes")
pool = Pool(4)
pool.map(getNodeAttributes, range(len(G)))
pool.close()
pool.join()
et.dump(network)
<network name="Petersen Graph">
<nodes/>
</network>