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

在 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时,也很快.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值