Pickle转储对象引发“超出最大递归深度”错误

时间:2016-01-13 18:02:59

标签: python pickle

我正在尝试使用Linked List为我的某个驱动器编制索引。每个节点包含路径的变量和该特定路径中所有文件夹和目录list。但是,我查看了错误,并且在达到 164(读取注释的大小之前,它没有显示任何错误。大于此,它提出了前面提到的错误。链表包含大约5500个节点。

注意:我尝试在程序开头使用 sys.setrecursionlimit(x) ,它可以达到757个节点,以便成功转储对象通过设置 pickle 来加载 sys.setrecursionlimit(4558) 。增加此限制, linklist 对象中的节点总数阈值没有变化,可以成功转储到文件中而不会出现标题错误。

其他注意事项:我将 import 包从 pickle 替换为 {{1} cPickle 可以作为对象存储到文件中的节点的阈值数量急剧增加到 4470 。我已相应更改了我的代码。

这是我的代码,它进一步简化,以便用户使其可读并且基本上坚持这一点。我已经省略了许多重要的函数和代码部分,据我所知,它们与错误无关。

sys.setrecursionlimit(13438)

以下是请求的Stack Trace:

1:这是我存储大小> 164的import cPickle as pickle #originally pickle import os import time import sys sys.setrecursionlimit(13438) class Link(object): def __init__(self, path, dir, file): self.path = path self.dir = dir self.file = file self.next = None def setNext(self, next): self.next = next def getPath(self): return self.path def getData(self): print "Node Visited." def containsDir(self, dir): pass class LinkList(object): def __init__(self): self.head = None self.tail = None self.size = 0 def insert(self, path, dir, file): val = Link(path, dir, file) if self.head is None: self.head = val self.tail = val else: val.setNext(self.head) self.head = val self.size = self.size + 1 def insert_bottom(self, path, dir, file): new_val = Link(path, dir, file) if self.head == None: self.head = new_val; self.tail = self.head; else: self.tail.setNext(new_val); self.tail = new_val; self.size = self.size + 1 def view(self): temp = self.head while temp != None: print temp.getData() temp = temp.next linklist = LinkList() for (path,dir,files) in os.walk("E:\\"): if(dir!=[] or files!=[]): linklist.insert_bottom(path, dir, files) if(linklist.size>=4477): #This is the threshold of storing this object using cPickle! break print linklist.size #linklist.view() output = open("list", "wb") pickle.dump(linklist, output, pickle.HIGHEST_PROTOCOL) #-1 = pickle.HIGHEST_PROTOCOL output.close() input = open("list", "rb") obj = pickle.load(input) 时(这会重复很多次,所以我发布了重复堆栈的第一部分和最后部分迹线):

linkedlist

2 :(已解决)当我将Traceback (most recent call last): File "temp.py", line 63 in <module> pickle.dump(linklist, output, pickle.HIGHEST_PROTOCOL) #-1 = pickle.HIGHEST_PROTOCOL File "C:\Python27\lib\pickle.py", line 1376, in dump Pickler(file, protocol).dump(obj) File "C:\Python27\lib\pickle.py", line 224, in dump self.save(obj) File "C:\Python27\lib\pickle.py", line 331, in save self.save_reduce(obj=obj, *rv) File "C:\Python27\lib\pickle.py", line 425, in save_reduce save(state) File "C:\Python27\lib\pickle.py", line 286, in save f(self, obj) # Call unbound method with explicit self File "C:\Python27\lib\pickle.py", line 655, in save_dict self._batch_setitems(obj.iteritems()) File "C:\Python27\lib\pickle.py", line 687, in _batch_setitems save(v) ... ... ... ... File "C:\Python27\lib\pickle.py", line 425, in save_reduce save(state) File "C:\Python27\lib\pickle.py", line 286, in save f(self, obj) # Call unbound method with explicit self File "C:\Python27\lib\pickle.py", line 655, in save_dict self._batch_setitems(obj.iteritems()) File "C:\Python27\lib\pickle.py", line 686, in _batch_setitems save(k) File "C:\Python27\lib\pickle.py", line 271, in save pid = self.persistent_id(obj) RuntimeError: maximum recursion depth exceeded 的对象存储到164或更小时,这是堆栈跟踪。 在加载文件之前缺少output.close()。

linkedlist

0 个答案:

没有答案