how to handle big loops in python?

Question:

This is more to do with my code as i’m sure i’m doing something wrong

all i’m trying to do is call a main function to repeated itself with a recursive number that at some point i’ll reset.

here is my code

sys.setrecursionlimit(8000)
didMenuFinish = 0

def mainThread(interval):
    time.sleep(0.2)
    print("interval is: " + str(interval))
    
    if (interval < 1650):
        raceTrack(interval)
    if (interval == 1680):
        resetButtons()
        restartRace()


    if didMenuFinish==0:
        mainThread(interval + 1)
    else:
        didMenuFinish = 0
        mainThread(0)

time.sleep(4)
keyboard.press(Key.enter)
keyboard.release(Key.enter)
mainThread(0)

At first the main thread runs fine, but somewhere in the 2500 interval +- it just stops with the error code for stack overflow

Process finished with exit code -1073741571 (0xC00000FD)

I am using Windows and pyCharm

do you know how I can handle that ? or am i doing something wrong ?

Asked By: Chief Madog

||

Answers:

You are most likely recursively looping forever. You recursively call mainThread in these parts:

def mainThread(interval):
    # ...
    if didMenuFinish == 0:
        mainThread(interval + 1)
    else:
        didMenuFinish = 0
        mainThread(0)

But you never return to the caller. You need a base case that stops the recursive loop


Consider this example of a recursive factorial function:

def fac(x):
    if x <= 1:
        return 1
    return x*fac(x-1)

The base case (terminating case) is when x <= 1. and this will be reached eventually as fac(x-1) implies that x is decreasing towards the conditional x <= 1. i.e. you need to have some sort of case where recursion is not needed. Your current function is recursively equivalent to:

def foo():
    foo()

Perhaps infinitely looping and locally updating variables will work?

def mainThread(interval):
    while True:
        time.sleep(0.2)
        print(f"interval is: " {interval})
        
        if interval < 1650:
            raceTrack(interval)
        if interval == 1680:
            resetButtons()
            restartRace()


        if didMenuFinish == 0:
            # No need to call the function again. just change the local variable
            interval += 1
        else:
            didMenuFinish = 0
            # Same here
            interval = 0
Answered By: Freddy Mcloughlan

You are getting a stack overflow error because your recursive calls never terminate.

Try using a loop instead:

def mainThread(interval):
    while True:
        time.sleep(0.2)
        print("interval is: " + str(interval))
        
        if (interval < 1650):
            raceTrack(interval)
        if (interval == 1680):
            resetButtons()
            restartRace()

        if didMenuFinish==0:
            interval += 1
        else:
            didMenuFinish = 0
            interval = 0
Answered By: fafl
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.