多处理永远以100%运行

时间:2013-12-04 00:10:14

标签: python multiprocessing networkx

有人可以解释原因:

import networkx as nx
import time
import multiprocessing as mp

def work(item):
    pths = []
    for path in nx.all_simple_paths(G, item, endNode,cutoff=maxLen-1):
        path.insert(0,startNode)
        pths.append(path)
    return pths

def init_worker(Gr, st,ed, cl):
    global G, startNode, endNode, maxLen

    print "process initializing", mp.current_process()
    G, startNode, endNode, maxLen = Gr, st, ed, cl
    G.remove_node(startNode)

def multi_core(graph, sourceChem, sinkChem, maxLen, minLen):
    paths = []
    startNeighbours = graph[sourceChem].keys()
    p = mp.Pool(initializer=init_worker, initargs=(graph,sourceChem, sinkChem, 9))
    paths = p.map(work, startNeighbours, chunksize=(len(startNeighbours)/mp.cpu_count()))
    for i in paths:
        if len(i) != 0:
            for j in i:
                paths.append(j)        
    p.close()
    p.join()
    return paths

if __name__ == "__main__":
    print multi_core(nx.read_graphml("RESTKegg.graphml"), 'C00025', 'C00029', 4, 2)

永远运行所有核心的100%核心使用率?我使用的代码与此类似但在multi_core中没有return,是什么导致了这个问题?

修改:正在运行:

graph = nx.read_graphml("minimal.graphml")
startNeighbours = graph['C00030'].keys()
init_worker(graph, 'C00030', 'C00072', 4)
for i in startNeighbours:
    work(i)

作为无线版本返回正常。

更新:我构建了一个测试台,用于从程序的其余部分中删除代码,看起来代码在程序之外正常工作,并且它似乎与我的Kivy有问题前端。

0 个答案:

没有答案