0

我正在将图形对象写入 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>
4

1 回答 1

1

使用队列 ( multiprocessing.Queue) 收集工作进程的结果。请参阅此问题的答案:在多个进程之间共享结果队列

也就是说,我不确定它对您的情况有多大帮助,因为 XML 文件需要按顺序读取和解析,并且元素树会很大。不过试一试...

于 2014-09-18T17:24:52.193 回答