python递归深度报错--RuntimeError: maximum recursion depth exceeded

本文介绍了一种在使用Python编写爬虫过程中遇到的递归深度限制问题,详细解析了问题产生的原因并提供了解决方案。

代码上传至https://github.com/gatieme/AderXCoding/blob/master/python/error/depth-exceeded.py

问题


这段时间用Python写了一个爬虫脚本,在抓取页面采用广度优先遍历抓取。但是当遍历到1000左右时就会出现莫名其妙的错误,
通过pdb调试发现是:

RuntimeError: maximum recursion depth exceeded

这里写图片描述

解析


发现python默认的递归深度是很有限的(默认是1000),因此当递归深度超过999的样子,就会引发这样的一个异常。

于是我们写了下面的代码进行测试,

在代码中函数func递归的调用自己,并且不退出
于是程序显然陷入一个死循环一样的递归

#!/usr/bin/env python
#coding=utf-8

import sys

def func(depth):
    depth += 1
    print "Now the depth is %d" % depth
    func(depth)


if __name__ == "__main__" :

    func(0)

程序运行的结果
这里写图片描述
我们可以捕获到开始异常的位置
这里写图片描述
我们可以看到当递归深度超过999达到1000的时候,引发了这个异常
解决的方式是手工设置递归调用深度,方式为

解决


可以修改递归深度的值,让它变大大一点

import sys   
sys.setrecursionlimit(1000000) #例如这里设置为一百万  

但是这个问题治标不治本,最好的办法还是优化自己的代码。。。。

在unet模型中,我的imgs是三个波段的真彩色rgb遥感影像,masks是分了5个等级的lst栅格图像。出现报错,ERROR:root:maximum recursion depth exceeded in comparison ERROR:root:Error processing files: data/imgs\4743.tif and data/masks\4743_mask.tif ERROR:root:maximum recursion depth exceeded in comparison ERROR:root:Error processing files: data/imgs\6452.tif and data/masks\6452_mask.tif ERROR:root:maximum recursion depth exceeded in comparison ERROR:root:Error processing files: data/imgs\3449.tif and data/masks\3449_mask.tif ERROR:root:maximum recursion depth exceeded in comparison ERROR:root:Error processing files: data/imgs\6266.tif and data/masks\6266_mask.tif ERROR:root:maximum recursion depth exceeded in comparison ERROR:root:Error processing files: data/imgs\5923.tif and data/masks\5923_mask.tif ERROR:root:Error processing files: data/imgs\4737.tif and data/masks\4737_mask.tif ERROR:root:maximum recursion depth exceeded in comparison ERROR:root:maximum recursion depth exceeded in comparison ERROR:root:Error processing files: data/imgs\3448.tif and data/masks\3448_mask.tif ERROR:root:maximum recursion depth exceeded in comparison ERROR:root:Error processing files: data/imgs\6265.tif and data/masks\6265_mask.tif ERROR:root:maximum recursion depth exceeded in comparison ERROR:root:Error processing files: data/imgs\5918.tif and data/masks\5918_mask.tif ERROR:root:maximum recursion depth exceeded in comparison ERROR:root:Error processing files: data/imgs\6453.tif and data/masks\6453_mask.tif ERROR:root:maximum recursion depth exceeded in comparison ERROR:root:Error processing files: data/imgs\3449.tif and data/masks\3449_mask.tif ERROR:root:maximum recursion depth exceeded in comparison ERROR:root:Error processing files: data/imgs\6266.tif and data/masks\6266_mask.tif ERROR:root:Error processing files: data/imgs\1382.tif and data/masks\1382_mask.tif ERROR:root:maximum recursion depth exceeded in comparison ERROR:root:maximum recursion depth exceeded in comparison ERROR:root:Error processing files: data/imgs\6451.tif and data/masks\6451_mask.tif ERROR:root:maximum recursion depth exceeded in comparison ERROR:root:Error processing files: data/imgs\1381.tif and data/masks\1381_mask.tif ERROR:root:maximum recursion depth exceeded in comparison ERROR:root:Error processing files: data/imgs\3443.tif and data/masks\3443_mask.tif ERROR:root:maximum recursion depth exceeded in comparison
最新发布
07-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安果移不动

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值