[Tutor] recursion depth

eryksun eryksun at gmail.com
Wed Jan 8 22:23:06 CET 2014


On Wed, Jan 8, 2014 at 3:25 PM, Keith Winston <keithwins at gmail.com> wrote:
> I've been playing with recursion, it's very satisfying.
>
> However, it appears that even if I sys.setrecursionlimit(100000), it blows
> up at about 24,000 (appears to reset IDLE). I guess there must be a lot of
> overhead with recursion, if only 24k times are killing my memory?

CPython recursion is limited by the thread's stack size, since
evaluating a Python frame requires calling PyEval_EvalFrameEx. The
default stack size on Windows is 1 MiB, and on Linux RLIMIT_STACK is
typically set at 8 MiB (inspect this w/ the stdlib's resource module).

You can create a worker thread with a larger stack using the threading
module. On Windows the upper limit is 256 MiB, so give this a try:

    import sys
    import threading

    MiB = 2 ** 20
    threading.stack_size(256 * MiB)

    sys.setrecursionlimit(100000)

    t = threading.Thread(target=your_function)
    t.start()

I'm not saying this is a good solution in general. It's just something
to play around with, and may help in a pinch.


More information about the Tutor mailing list