What is the maximum recursion depth in Python, and how to increase it?

前端 未结 17 2874
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-21 04:14

I have this tail recursive function here:

def recursive_function(n, sum):
    if n < 1:
        return sum
    else:
        return recursive_function(n-1         


        
17条回答
  •  -上瘾入骨i
    2020-11-21 05:15

    resource.setrlimit must also be used to increase the stack size and prevent segfault

    The Linux kernel limits the stack of processes.

    Python stores local variables on the stack of the interpreter, and so recursion takes up stack space of the interpreter.

    If the Python interpreter tries to go over the stack limit, the Linux kernel makes it segmentation fault.

    The stack limit size is controlled with the getrlimit and setrlimit system calls.

    Python offers access to those system calls through the resource module.

    sys.setrecursionlimit mentioned e.g. at https://stackoverflow.com/a/3323013/895245 only increases the limit that the Python interpreter self imposes on its own stack size, but it does not touch the limit imposed by the Linux kernel on the Python process.

    Example program:

    main.py

    import resource
    import sys
    
    print resource.getrlimit(resource.RLIMIT_STACK)
    print sys.getrecursionlimit()
    print
    
    # Will segfault without this line.
    resource.setrlimit(resource.RLIMIT_STACK, [0x10000000, resource.RLIM_INFINITY])
    sys.setrecursionlimit(0x100000)
    
    def f(i):
        print i
        sys.stdout.flush()
        f(i + 1)
    f(0)
    

    Of course, if you keep increasing setrlimit, your RAM will eventually run out, which will either slow your computer to a halt due to swap madness, or kill Python via the OOM Killer.

    From bash, you can see and set the stack limit (in kb) with:

    ulimit -s
    ulimit -s 10000
    

    The default value for me is 8Mb.

    See also:

    • Setting stacksize in a python script
    • What is the hard recursion limit for Linux, Mac and Windows?

    Tested on Ubuntu 16.10, Python 2.7.12.

提交回复
热议问题