0

I am doing a program that gives the user attention each 25 min, then gives him 5 min break, everything was well for testing the code with small number of time(less than 2 min) but when I test it in real 25 min ,it gives me this error:

RecursionError: maximum recursion depth exceeded

what I thought that I need to change recursion depth, then I changed it by:

sys.setrecursionlimit(1000000)

then it really exceeded the 2 min but when it reached about 10 min the program just killed!

I guess it is because the program makes a big space of memory or something like that but how can I fix this problem!!?

I will try to give you the main function briefly so you can understand the idea::

sec=1500
count=0
def run():
    global sec
    global count
    m,s=divmod(sec,60)   
    if sec >=0:
        m,s=divmod(sec,60)
        lblmin.config(text=str(m).zfill(2))
        lblsec.config(text=str(s).zfill(2))
        sec -= 1
        if sec==-1:
            count += 1
            if count%2 ==0 :   #to know if the timer will do 5 min or 25 min
                confstr=messagebox.showinfo("stert?","timer will start now")
                if confstr:
                    sec=1500
            else: #for break
                runbreak=False
                if runbreak == False:
                    root.attributes("-topmost", True)
                    ask=messagebox.askyesno("break","break for 5 min")
                    runbreak=True
                if runbreak:
                    sec=300


    root.after(1000,run)
    root.mainloop()

maybe (just what I think) it is because of the huge time that lblmin and lblsec are changing, but I really don't have any idea to fix this problem.

please help:(

M M
  • 13
  • 4
  • First you have to understand [Event-Driven Programming](http://eventdrivenpgm.sourceforge.net/) and [Tkinter understanding mainloop](https://stackoverflow.com/questions/29158220/tkinter-understanding-mainloop) and [runtime-error-in-python-maximum-recursion-depth-exceeded](https://stackoverflow.com/questions/22717248) – stovfl Jan 19 '20 at 16:44
  • sir, I am trying to understand it but can you identify the exact problem in my code? do you recommend any procedure to solve this problem :{ – M M Jan 19 '20 at 17:02
  • In addition to reading what @stovfl suggested, can you share your entire program? I can see multiple things which should be improved, there may be other issues in the rest of the code. – AMC Jan 19 '20 at 18:55
  • You need to move the statement `root.mainloop()` out of `run()` function to the end of the script. – acw1668 Jan 21 '20 at 13:16

1 Answers1

0

Python is not optimised for tail recursion, there is an explanation in this article.

Practically this means that python limits the number of times a function can call itself (if it didn't there would eventually be a stack overflow). You can change that limit, as you have using setrecursionlimit, but fundamentally the limit is always going to exist.

I suggest that you refactor the code to use a while loop to keep cycling round instead.

Sam Broster
  • 542
  • 5
  • 15