启动django的服务时候报错RuntimeError: maximum recursion depth exceeded in cmp

原文:http://blog.youkuaiyun.com/yingzinanfei/article/details/54862551

启动django服务时报错:

RuntimeError: maximum recursion depth exceeded in cmp

解决方法:

原因出自Python\Lib\fuctools.py 

convert = {
    '__lt__': [('__gt__', lambda self, other: other < self),
               ('__le__', lambda self, other: not other < self),
               ('__ge__', lambda self, other: not self < other)],
    '__le__': [('__ge__', lambda self, other: other <= self),
               ('__lt__', lambda self, other: not other <= self),
               ('__gt__', lambda self, other: not self <= other)],
    '__gt__': [('__lt__', lambda self, other: other > self),
               ('__ge__', lambda self, other: not other > self),
               ('__le__', lambda self, other: not self > other)],
    '__ge__': [('__le__', lambda self, other: other >= self),
               ('__gt__', lambda self, other: not other >= self),
               ('__lt__', lambda self, other: not self >= other)]
}
修改为:

convert = {
		'__lt__': [('__gt__', lambda self, other: not (self < other or self == other)),
				   ('__le__', lambda self, other: self < other or self == other),
				   ('__ge__', lambda self, other: not self < other)],
		'__le__': [('__ge__', lambda self, other: not self <= other or self == other),
				   ('__lt__', lambda self, other: self <= other and not self == other),
				   ('__gt__', lambda self, other: not self <= other)],
		'__gt__': [('__lt__', lambda self, other: not (self > other or self == other)),
				   ('__ge__', lambda self, other: self > other or self == other),
				   ('__le__', lambda self, other: not self > other)],
		'__ge__': [('__le__', lambda self, other: (not self >= other) or self == other),
				   ('__gt__', lambda self, other: self >= other and not self == other),
				   ('__lt__', lambda self, other: not self >= other)]
	}



### 解决 Django 数据库迁移时遇到的 `RecursionError` 错误 当执行 Django 的数据库迁移命令并遇到 `RecursionError: maximum recursion depth exceeded while calling a Python object` 这样的错误时,通常意味着存在无限递归调用的情况。以下是几种可能的原因及其解决方案: #### 增加 Python 递归深度限制 有时,默认的递归深度不足以处理某些复杂的应用逻辑或数据结构。可以通过增加 Python 的递归深度来尝试解决问题。 ```python import sys sys.setrecursionlimit(100000) # 设置更高的递归深度限制[^2] ``` 需要注意的是,这种方法只是临时措施,并未从根本上消除引发递归过深的根本原因。 #### 检查 Celery 和 Redis 配置 如果应用中集成了 Celery 并使用 Redis 作为消息代理,则应验证配置文件中的连接字符串是否正确无误,特别是认证部分。不正确的密码可能会导致循环重试从而触发此异常。 ```python BROKER_URL = 'redis://:<correct_password>@<host>:6379/<db>' CELERY_RESULT_BACKEND = 'redis://:<correct_password>@<host>:6379/<another_db>' ``` 确保上述 URL 中 `<correct_password>` 是实际有效的Redis服务器访问凭证[^3]。 #### 排除模型定义中的潜在问题 检查应用程序内的所有模型类定义,尤其是那些涉及继承、多态关系或多表关联的地方。任何不当的设计都可能导致意外的行为模式,在极端情况下甚至会造成栈溢出。 对于复杂的字段选项或是元属性设置也要格外小心;比如自定义管理器方法里是否有不必要的递归调用等情形。 #### 使用调试工具定位具体位置 利用 Python 自带或其他第三方提供的调试辅助功能(如 pdb),逐步跟踪程序流直至找到确切抛出异常的位置。这有助于更精准地识别和修正引起该问题的具体代码片段。 通过以上几个方面入手排查可以有效减少乃至彻底杜绝此类错误的发生概率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值