查看: 1426|回复: 1

Exception RuntimeError: 'maximum recursion depth exceeded'

[复制链接]

9

主题

74

帖子

185

积分

注册会员

Rank: 2

积分
185
发表于 2018-9-18 18:43:57 | 显示全部楼层 |阅读模式
我有以下递归代码,在每个节点我调用sql查询来获取属于父节点的节点。
这是错误:
Exception RuntimeError: 'maximum recursion depth exceeded' in <bound method DictCursor.__del__ of <MySQLdb.cursors.DictCursor object at 0x879768c>> ignored

RuntimeError: maximum recursion depth exceeded while calling a Python object
Exception AttributeError: "'DictCursor' object has no attribute 'connection'" in <bound method DictCursor.__del__ of <MySQLdb.cursors.DictCursor object at 0x879776c>> ignored
我调用以获取sql结果的方法:
  1. def returnCategoryQuery(query, variables={}):
  2.     cursor = db.cursor(cursors.DictCursor);
  3.     catResults = [];
  4.     try:
  5.         cursor.execute(query, variables);
  6.         for categoryRow in cursor.fetchall():
  7.             catResults.append(categoryRow['cl_to']);
  8.         return catResults;
  9.     except Exception, e:
  10.         traceback.print_exc();
复制代码

我实际上对上述方法没有任何问题,但我还是把它放在了正确的问题概述上。
递归代码:
  1. def leaves(first, path=[]):
  2.     if first:
  3.         for elem in first:
  4.             if elem.lower() != 'someString'.lower():
  5.                 if elem not in path:
  6.                     queryVariable = {'title': elem}
  7.                     for sublist in leaves(returnCategoryQuery(categoryQuery, variables=queryVariable)):
  8.                         path.append(sublist)
  9.                         yield sublist
  10.                     yield elem
复制代码

调用递归函数
  1. for key, value in idTitleDictionary.iteritems():
  2.     for startCategory in value[0]:
  3.         print startCategory + " ==== Start Category";
  4.         categoryResults = [];
  5.         try:
  6.             categoryRow = "";
  7.             baseCategoryTree[startCategory] = [];
  8.             #print categoryQuery % {'title': startCategory};
  9.             cursor.execute(categoryQuery, {'title': startCategory});
  10.             done = False;
  11.             while not done:
  12.                 categoryRow = cursor.fetchone();
  13.                 if not categoryRow:
  14.                     done = True;
  15.                     continue;
  16.                 rowValue = categoryRow['cl_to'];
  17.                 categoryResults.append(rowValue);
  18.         except Exception, e:
  19.             traceback.print_exc();
  20.         try:
  21.             print "Printing depth " + str(depth);
  22.             baseCategoryTree[startCategory].append(leaves(categoryResults))
  23.         except Exception, e:
  24.             traceback.print_exc();
复制代码

打印字典的代码,
  1. print "---Printing-------"
  2. for key, value in baseCategoryTree.iteritems():
  3.     print key,
  4.     for elem in value[0]:
  5.         print elem + ',';
  6.     raw_input("Press Enter to continue...")
  7.     print
复制代码

如果递归太深,我应该在调用递归函数时收到错误,但是当我打印字典时出现此错误。
回复

使用道具 举报

11

主题

80

帖子

199

积分

注册会员

Rank: 2

积分
199
发表于 2018-9-18 18:44:30 | 显示全部楼层
可以增加允许的堆栈深度 - 这样,可以进行更深层次的递归调用,如下所示:
import sys
sys.setrecursionlimit(10000) # 10000 is an example, try with different values
...但我建议首先尝试优化代码,例如,使用迭代而不是递归。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表