我正在将graph对象写入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)
^{pr2}$
# 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>
使用队列(
multiprocessing.Queue
)收集工作进程的结果。请看这个问题的答案:Sharing a result queue among several processes。在也就是说,我不确定它对您的情况有多大帮助,因为XML文件需要按顺序读取和解析,而且元素树将非常大。但试一试。。。在
相关问题 更多 >
编程相关推荐