在Windows(至少),sys.setrecursionlimit
不是完整的故事。硬限制以每個線程爲基礎,並且您需要致電threading.stack_size
並在達到一定限制後創建新線程。 (我認爲1MB,但不確定)我已經使用這種方法將其增加到了64MB的堆棧。
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()
我還沒有嘗試過,看看有什麼限制有可能會在threading.stack_size
,但隨時嘗試......這就是你需要看看。
總之,sys.setrecursionlimit
只是解釋器本身強制執行的限制。 threading.stack_size
可讓您操縱操作系統施加的實際限制。如果你首先達到後者的限制,Python將會完全崩潰。
不還取決於你有多少內存等等。 – Justin 2010-05-26 22:18:11
可能相當於「什麼是{os}上最大可能的setrlimit堆棧大小」。對於Linux,我敢打賭幾乎所有的用戶態RAM,但我不知道。值可以通過'resource.setrlimit'操作:http://stackoverflow.com/questions/5061582/setting-stacksize-in-a-python-script – 2017-01-28 23:51:06