【发布时间】:2017-03-14 14:25:36
【问题描述】:
我正在处理大型树,需要增加 Python 2.7 的递归限制。
使用sys.setrecursionlimit(10000) 会使我的内核崩溃,所以我想我需要增加堆栈大小。
但是我不知道堆栈大小应该有多大。我试过100 MiB 这样threading.stack_size(104857600),但内核仍然死机。给它1 GiB 会引发错误。
我还没有使用过threading 模块,所以当我将上述语句放在脚本开头时,我是否使用错了?我没有做任何并行处理,一切都在同一个线程中完成。
我的电脑有 128 GB 物理 RAM,运行 Windows 10,Spyder 中的 iPython 控制台。
显示的错误很简单:
内核死机,正在重启
仅此而已。
编辑:
重现问题的完整代码。树的构建工作得很好,认为它需要很长时间,内核仅在递归执行treeToDict() 将整个树读入字典时死亡。也许该函数的代码有问题。该树是非二叉树:
import pandas as pd
import threading
import sys
import random as rd
import itertools as it
import string
threading.stack_size(104857600)
sys.setrecursionlimit(10000)
class treenode:
# class to build the tree
def __init__(self,children,name='',weight=0,parent=None,depth=0):
self.name = name
self.weight = weight
self.children = children
self.parent = parent
self.depth = depth
self.parentname = parent.name if parent is not None else ''
def add_child(node,name):
# add element to the tree
# if it already exists at the given node increase weight
# else add a new child
for i in range(len(node.children)):
if node.children[i].name == name:
node.children[i].weight += 1
newTree = node.children[i]
break
else:
newTree = treenode([],name=name,weight=1,parent=node,depth=node.depth+1)
node.children.append(newTree)
return newTree
def treeToDict(t,data):
# read the tree into a dictionary
if t.children != []:
for i in range(len(t.children)):
data[str(t.depth)+'_'+t.name] = [t.name, t.children[i].name, t.depth, t.weight, t.parentname]
else:
data[str(t.depth)+'_'+t.name] = [t.name, '', t.depth, t.weight, t.parentname]
for i in range(len(t.children)):
treeToDict(t.children[i],data)
# Create random dataset that leads to very long tree branches:
# A is an index for each set of data B which becomes one branch
rd.seed(23)
testSet = [''.join(l) for l in it.combinations(string.ascii_uppercase[:20],2)]
A = []
B = []
for i in range(10):
for j in range(rd.randint(10,6000)):
A.append(i)
B.append(rd.choice(testSet))
dd = {"A":A,"B":B}
data = pd.DataFrame(dd)
# The maximum length should be above 5500, use another seed if it's not:
print data.groupby('A').count().max()
# Create the tree
root = treenode([],name='0')
for i in range(len(data.values)):
if i == 0:
newTree = add_child(root,data.values[i,1])
oldses = data.values[i,0]
else:
if data.values[i,0] == oldses:
newTree = add_child(newTree,data.values[i,1])
else:
newTree = add_child(root,data.values[i,1])
oldses = data.values[i,0]
result={}
treeToDict(root,result)
PS:我知道treeToDict() 函数有问题,因为它可能会覆盖条目,因为可能有重复的键。然而,对于这个错误,这个错误并不重要。
【问题讨论】:
-
请向我们展示您遇到的实际错误,而不是试图描述它们。你显然不会从
sys.setrecursionlimit(10000)崩溃内核。也许提供有关您正在运行的操作系统的信息也很重要。 -
添加了一些信息。
标签: python-2.7 recursion stack ipython spyder