반응형
Python : 최대 재귀 깊이 초과
다음 재귀 코드가 있으며 각 노드에서 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 결과를 얻기 위해 호출하는 방법 :
def returnCategoryQuery(query, variables={}):
cursor = db.cursor(cursors.DictCursor);
catResults = [];
try:
cursor.execute(query, variables);
for categoryRow in cursor.fetchall():
catResults.append(categoryRow['cl_to']);
return catResults;
except Exception, e:
traceback.print_exc();
나는 실제로 위의 방법에 문제가 없지만 질문에 대한 적절한 개요를 제공하기 위해 어쨌든 넣었습니다.
재귀 코드 :
def leaves(first, path=[]):
if first:
for elem in first:
if elem.lower() != 'someString'.lower():
if elem not in path:
queryVariable = {'title': elem}
for sublist in leaves(returnCategoryQuery(categoryQuery, variables=queryVariable)):
path.append(sublist)
yield sublist
yield elem
재귀 함수 호출
for key, value in idTitleDictionary.iteritems():
for startCategory in value[0]:
print startCategory + " ==== Start Category";
categoryResults = [];
try:
categoryRow = "";
baseCategoryTree[startCategory] = [];
#print categoryQuery % {'title': startCategory};
cursor.execute(categoryQuery, {'title': startCategory});
done = False;
while not done:
categoryRow = cursor.fetchone();
if not categoryRow:
done = True;
continue;
rowValue = categoryRow['cl_to'];
categoryResults.append(rowValue);
except Exception, e:
traceback.print_exc();
try:
print "Printing depth " + str(depth);
baseCategoryTree[startCategory].append(leaves(categoryResults))
except Exception, e:
traceback.print_exc();
사전을 인쇄하는 코드,
print "---Printing-------"
for key, value in baseCategoryTree.iteritems():
print key,
for elem in value[0]:
print elem + ',';
raw_input("Press Enter to continue...")
print
재귀가 너무 깊으면 재귀 함수를 호출 할 때 오류가 발생하지만 사전을 인쇄 할 때이 오류가 발생합니다.
허용되는 스택 깊이를 늘릴 수 있습니다. 이렇게하면 다음과 같이 더 깊은 재귀 호출이 가능합니다.
import sys
sys.setrecursionlimit(10000) # 10000 is an example, try with different values
...하지만 먼저 재귀 대신 반복을 사용하여 코드를 최적화하는 것이 좋습니다.
참고URL : https://stackoverflow.com/questions/8177073/python-maximum-recursion-depth-exceeded
반응형
'IT박스' 카테고리의 다른 글
LINUX에서 .a 라이브러리 / 아카이브 32 비트 또는 64 비트를 확인하십시오. (0) | 2020.10.12 |
---|---|
CSS : 이미지 주위에 흰색 광선 만들기 (0) | 2020.10.12 |
xampp localhost를 다른 폴더 (xampp 폴더 외부)로 변경하는 방법은 무엇입니까? (0) | 2020.10.12 |
LINQ to Entities는 'System.String Format (System.String, System.Object, System.Object)'메서드를 인식하지 않습니다. (0) | 2020.10.12 |
다른 순서로 동일한 요소가있는 두 개의 JSON 객체를 동일한 순서로 비교하는 방법은 무엇입니까? (0) | 2020.10.12 |