gpt4 book ai didi

python - 确定 Python 的最大有效 setrecursionlimit 值

转载 作者:太空狗 更新时间:2023-10-30 01:33:15 26 4
gpt4 key购买 nike

在 Python 2 文档中,sys 库包含以下内容(粗体部分是我的编辑):

sys.setrecursionlimit(限制)

Set the maximum depth of the Python interpreter stack to limit. This limit prevents infinite recursion from causing an overflow of the C stack and crashing Python.

The highest possible limit is platform-dependent. A user may need to set the limit higher when she has a program that requires deep recursion and a platform that supports a higher limit. This should be done with care, because a too-high limit can lead to a crash.

这是什么意思?这只是一般性的“确保您有足够的内存来处理额外的堆栈空间”语句,还是有特定的“每个堆栈帧”大小可用于计算所需的内存值? Python 无法获取空间时会发生什么情况?

最佳答案

为什么让我们找出答案:

me@host$ docker run -m 4MB --cpuset-cpus=0 -it --rm python:3.5.1
Python 3.5.1 (default, Dec 9 2015, 00:12:22)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys, struct
>>> maxint = 2 ** (struct.Struct('i').size * 8 - 1) - 1
>>> sys.setrecursionlimit(maxint)
>>> def goodbye_world():
... goodbye_world()
...
>>> goodbye_world()
me@host$

拜托。看起来它会使 Python 崩溃。当您只给它 4MB 的 RAM 时,速度也很快。

关于python - 确定 Python 的最大有效 setrecursionlimit 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34883825/

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