gpt4 book ai didi

python - 异步函数中的 "RecursionError: maximum recursion depth exceeded in comparison"

转载 作者:太空宇宙 更新时间:2023-11-04 04:52:12 26 4
gpt4 key购买 nike

我为 Tornado RequestHandler 编写了一个辅助装饰器:

def return_response(method):
@wraps(method)
async def wrapper(self, *args, **kwargs):
response = method(self, *args, **kwargs)
if inspect.isawaitable(response):
response = await response
self.set_status(response.status)
self.write(response.body)
return wrapper

由于某种原因,它失败并出现以下错误:

Traceback (most recent call last):
File ".../.venv/lib/python3.6/site-packages/tornado/web.py", line 1511, in _execute
result = yield result
File ".../.venv/lib/python3.6/site-packages/tornado/gen.py", line 1055, in run
value = future.result()
File ".../.venv/lib/python3.6/site-packages/tornado/concurrent.py", line 238, in result
raise_exc_info(self._exc_info)
File "<string>", line 4, in raise_exc_info
File ".../.venv/lib/python3.6/site-packages/tornado/gen.py", line 307, in wrapper
yielded = next(result)
File "<string>", line 6, in _wrap_awaitable
File ".../src/http_handlers/decorators.py", line 106, in wrapper
response = await response
File ".../src/http_handlers/decorators.py", line 106, in wrapper
response = await response
File ".../src/http_handlers/decorators.py", line 106, in wrapper
response = await response
[Previous line repeated 959 more times]
File ".../src/http_handlers/decorators.py", line 104, in wrapper
response = method(self, *args, **kwargs)
RecursionError: maximum recursion depth exceeded in comparison

此错误仅在应用程序启动一段时间后出现。是 Python、Tornado 中的错误还是我的代码有误?有人遇到过同样的问题吗?

更新

我已经设法解决了这个问题。它在完全不同的地方。我曾经在 __new__ 方法中在运行时应用这个装饰器。我错误地将它应用于 cls 的方法,而不是实际实例。因此,每次创建类的新实例时,都会再次包装方法。

最佳答案

也许看看这个What is the maximum recursion depth in Python, and how to increase it?然而,答案不会是增加它。

当您不断从自身内部调用函数时,它会不断添加到堆栈中,从而不断填满内存。 Python 对此设置了限制,因此您不会遇到堆栈溢出。一段时间后,您似乎达到了该限制。

decorators.py106 行发生的任何事情都会导致同一方法在自身内部被一遍又一遍地调用。我不确定在没有看到完整用法的情况下我们是否能够说出到底发生了什么,但希望这能给您一些想法。

我建议尝试找到您的代码在哪里被递归调用,并想出一种方法来重构您的程序,以免发生这种情况。

关于python - 异步函数中的 "RecursionError: maximum recursion depth exceeded in comparison",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47974349/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com