I had a large issue where I needed thousands of layers of recursion, but python wouldn't let me. As I'm building this on a windows system, it also proved difficult to try and find the solution. In essence the recursion runs on the stack, and in non UNIX based systems it's hard to mess with the stack.

If you're looking for the most simple solution which works on UNIX:

import sys

However, as I mentioned, this did not work for me on windows. I needed to come up with solution or switch operating systems.

  • 2
    $\begingroup$ I’m voting to close this question because it is a general Python question which is unrelated to Blender. $\endgroup$ – Ray Mairlot May 17 '20 at 14:14
  • 2
    $\begingroup$ Hi. Please don't add 'solved' to the title. Questions are marked as solved when an answer is accepted. However, the point is slightly moot as this doesn't seem to be about Blender, which is what this site is focused on. $\endgroup$ – Ray Mairlot May 17 '20 at 14:16
  • $\begingroup$ should be closed, didnt realize i was posting to blender and not stackoverflow $\endgroup$ – Cole May 17 '20 at 18:29

To solve this, I had the wild idea of using multithreading to give me multiple stacks. I didn't honestly know if this would work before I tried it. I hate multithreading generally, but this is a very basic version of that. I'm sure others can improve on it. In my final product I implement multithreading into my backtracking algorithm.

import threading

def recurThread(x,y,z):
    if x <= 1:
        if z[0] != 1:
            new_y = []
            z[0] -= 1
            new_t = threading.Thread(target=recurThread, args=(900, new_y, z))

            y += new_y

    return recurThread(x-1, y, z)

y = []
z = [50]
x = threading.Thread(target=recurThread, args=(900, y, z))
total = 0
for i in y:
    total += i


This is the basic idea. Things to note are that if you want to get values back from the threads, you need to store them (and pass them) as mutable objects like lists.

The other thing to note if you're unfamiliar with threads (like I was) is that you need to .start() your thread. More importantly, .join() will wait for the thread to finish before continuing. This allows for the program to be run normally, just with this instance of a thread allowing you to cheat the stack limit.


Not the answer you're looking for? Browse other questions tagged or ask your own question.