在哪里可以看到为从递归限制中恢复而保留的“额外递归”堆栈量?

问题描述

这个问题是关于尝试了解python内部原理的问题。

考虑代码:

import sys

sys.setrecursionlimit(100)

def myfunc(n):
    print(n,sys.getrecursionlimit())
    try:
        myfunc(n+1)
    except Exception as e1:
        print('Cannot proceed')
        print(e1)
        try:
            myfunc(n+1)
        except Exception as e2:
            print('That went wrong')
            print(e2)

myfunc(0)

警告-此代码用于python内部研究,并导致python崩溃。导致实际的Fatal Python error: Cannot recover from stack overflow.

这是结果:

....
....
92 100
93 100
94 100
95 100
Cannot proceed
maximum recursion depth exceeded while calling a Python object
95 100
96 100
97 100
98 100
99 100
100 100
101 100
102 100
103 100
104 100
105 100
106 100
107 100
108 100
109 100
110 100
111 100
112 100
113 100
114 100
115 100
116 100
117 100
118 100
119 100
120 100
121 100
122 100
123 100
124 100
125 100
126 100
127 100
128 100
129 100
130 100
131 100
132 100
133 100
134 100
135 100
136 100
137 100
138 100
139 100
140 100
141 100
142 100
143 100
144 100
Fatal Python error: Cannot recover from stack overflow.

Current thread 0x0000379c (most recent call first):
  File "....test.py",line 7 in myfunc
  File "....test.py",line 9 in myfunc
....
....

这是我想像的大致情况:

  1. 在达到递归限制之前,该函数计数到接近100。接近100,而不是100,因为python本身使用了一些堆栈。
  2. 发生异常时,将实际递归限制设置为较高以允许恢复。
  3. 由于我的函数坚持保持递归,因此一段时间后它将达到新的限制。
  4. 但这一次,这种“扩展限制”恢复机制已用尽,并发生了致命的堆栈溢出。

我说得对吗?

我的问题:我可以使用等同于sys.getrecursionlimit()的东西在某些系统变量中看到“额外递归”堆栈的数量吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...