5

I know standard CPython has a limit on recursion depth, less than 1000 I think, so the below example code will fail with a "maximum recursion depth exceeded" error.

def rec_add(x):
    if x == 0:
        return x
    else:
        return x + add(x - 1)

print(rec_add(1000))

I heard Stackless Python supports infinite recursion depth, but if I run the above code with Stackless Python, it still reports a "maximum recursion depth exceeded" error. I think maybe I need to modify the code somehow to enable it to use the infinite recursion depth feature of Stackless Python?

Any idea how to do infinite recursions in Stackless Python? Thanks.

Note: I know how to increase standard CPython's recursion depth limit over 1000, and I know how to convert the above code to a simple iteration, or simply use the Gauss formula to calculate the sum, those are not what I'm looking for, and the above code is purely as an example.

EDIT: Like I already said in the "Note" part above (that I guess no one actually reads), I know how to increase CPython's recursion limit, and I know how to convert the example code into iterations or just a Gauss sum formula of n * (n + 1) / 2, I'm just asking here because I heard one of the great features of Stackless Python is that it enables infinite recursions, and I don't know how may I enable it for the example code.

EDIT2: I'm not sure if I got the idea of "Stackless Python supports infinite recursions" wrong, but here are some sources that says (or alludes to) that Stackless Python supports infinite recursions:

What are the drawbacks of Stackless Python?

https://bitbucket.org/stackless-dev/stackless/issues/96

https://stackless.readthedocs.io/en/3.6-slp/whatsnew/stackless.html

hellopeach
  • 924
  • 8
  • 15
  • Possible duplicate of [Python: Maximum recursion depth exceeded](https://stackoverflow.com/questions/8177073/python-maximum-recursion-depth-exceeded) – Klaus D. Mar 24 '19 at 06:04
  • 6
    Definitely not a duplicate, since I explicitly asked how to implement **infinite recursions** in **Stackless Python**, not how to increase the recursion depth limit over 1000 in standard CPython, which is what your linked question and its accepted answer there is about. – hellopeach Mar 24 '19 at 06:07
  • 1
    I think you are operating under a fundamental misconception ... I see nothing related to infinite recursion in the stackless docs ... it seems it has more to do with async/await type stuff (which is now builtin to python) – Joran Beasley Mar 24 '19 at 06:12
  • 2
    There is no infinite recursion. – Klaus D. Mar 24 '19 at 06:14
  • 1
    I've seem from a past question that says Stackless support infinite recursion: https://stackoverflow.com/questions/588958/what-are-the-drawbacks-of-stackless-python , also some docs online talks about infinite recursions and how to limit the memory usage: https://stackless.readthedocs.io/en/3.6-slp/whatsnew/stackless.html – hellopeach Mar 24 '19 at 06:19

1 Answers1

2

After fumbling around I got the following code based on an official example code from more than a decade ago here

https://bitbucket.org/stackless-dev/stacklessexamples/src/a01959c240e2aeae068e56b86b4c2a84a8d854e0/examples/?at=default

so I modified the recursive addition code to look like this

import stackless


def call_wrapper(f, args, kwargs, result_ch):
    result_ch.send(f(*args, **kwargs))


def call(f, *args, **kwargs):
    result_ch = stackless.channel()
    stackless.tasklet(call_wrapper)(f, args, kwargs, result_ch)
    return result_ch.receive()


def rec_add(n):
    if n <= 1:
        return 1
    return n + call(rec_add, n-1)


print(rec_add(1000000))

It works with large number like 1,000,000, I guess it is kind of an indirect recursion since the function calls another function which starts a tasklet that calls the function itself (or something like this).

Now I'm wondering if this is indeed the supposed way to implement an infinite recursion in Stackless Python, or are there more straight-forward/direct ways of doing it? Thanks.

hellopeach
  • 924
  • 8
  • 15