我有一个多处理脚本,如下所示:
from multiprocessing import Pool
currentDepth = 1
maxDepth = 3
nodeList = []
auxList = []
def grow():
global nodeList
global auxList
while(nodeList):
print("Iterate on: " + str(nodeList))
p.map(proc1, nodeList)
print("Repopulating nodeList: " + str(nodeList) + " from auxList: " + str(auxList))
nodeList = list(auxList)
nodeList.append(seed)
p = Pool(8)
grow()
proc1
函数调用proc2
函数,如下所示:
def proc1(currentNode):
val = int(currentNode.name)*2
if (val not in visited):
proc2(currentNode, val, currentNode.depth+1)
def proc2(currentNode, newVal, depth):
global currentDepth
global auxList
print("Next node value: " str(newVal))
print("Next node depth: " str(depth))
nextNode = Tree(name='{}'.format(newVal))
currentNode.add_child(nextNode)
if depth > currentDepth:
currentDepth = depth
print("Current Depth updated: " + str(currentDepth))
print("Maximum Depth: " + str(maxDepth))
if depth < maxDepth:
print("Appending to aux: " + str(nextNode.name)
auxList.append(nextNode)
print("auxList: " + str(auxList))
proc2
成功附加到auxList
,Iterate on: [Tree node '2' (0x7f3537cd61d)]
Next node value: 4
Node depth: 2
Current Depth updated: 2
Maximum Depth: 3
Appending to aux: 4
auxList: [Tree node '4' (0x7f79abec191)]
Repopulating nodeList: [Tree node '2' (0x7f79ac3171d)] from auxList: []
反过来应该每次迭代重新填充nodeList直到某个深度。打印输出如下:
auxList
一切都在正确编译,为什么 Ouseph MM, Li J, Chen HZ, Pécot T, Wenzel P, Thompson JC, Comstock
G, Chokshi V, Byrne M, Forde B, Chong JL, Huang K, Machiraju R, de
Bruin A, Leone G (2012) Atypical E2F repressors and activators
coordinate placental development. <i>Dev Cell</i>. 22(4):849-62
PMCID:PMC3483796
错过了它的条目?我对异步执行和我对全局变量的使用有一些怀疑,但我无法弄清楚问题是什么。提前谢谢大家!