Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
248 views
in Technique[技术] by (71.8m points)

python - What is the hard recursion limit for Linux, Mac and Windows?

Python's sys module provides a function setrecursionlimit that lets you change Python's maximum recursion limit. The docs say:

The highest possible limit is platform-dependent.

My question is: What is the highest possible limits for various platforms, under CPython? I would like to know the values for Linux, Mac and Windows.

UPDATE: Can we please avoid "You're doing it wrong" answers? I know that trying to do very deep recursion is usually a bad idea. I've considered the pros and cons in my specific situation and decided that I want to do it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

On Windows (at least), sys.setrecursionlimit isn't the full story. The hard limit is on a per-thread basis and you need to call threading.stack_size and create a new thread once you reach a certain limit. (I think 1MB, but not sure) I've used this approach to increase it to a 64MB stack.

import sys
import threading

threading.stack_size(67108864) # 64MB stack
sys.setrecursionlimit(2 ** 20) # something real big
                               # you actually hit the 64MB limit first
                               # going by other answers, could just use 2**32-1

# only new threads get the redefined stack size
thread = threading.Thread(target=main)
thread.start()

I haven't tried to see what limits there might be on threading.stack_size, but feel free to try... that's where you need to look.

In summary, sys.setrecursionlimit is just a limit enforced by the interpreter itself. threading.stack_size lets you manipulate the actual limit imposed by the OS. If you hit the latter limit first, Python will just crash completely.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...