0

I have the following codes which will be running 24 hours but only after 10-20 minutes I get error maximum recursion depth reached. My code is folowing

def startEnd():
    flag = 0
    f = open('file')
    lq = f.readlines()
    cur2 = lq[0]
    cur1 = datetime.datetime.now()
    while flag == 0:
        if cur1 == cur2: # cur2 is datetime read from file
            q.put(True)
            flag = 1
        else:
            flag = 0
    startEnd()

How can I avoid recursion in the following code? I need to come out of while loop since cur2 value changes.

My other question is that will following code will also lead to recursion depth error in the long run since my code need to be run 24 hours.

def planIncr():
    f=open('input.txt')
    lines=f.readlines()
    cycle_time = int(lines[1])
    f.close()
    q2.put(True)
    threading.Timer(cycle_time, planIncr).start()
2
  • Make them non-recursive is the simple answer. The best way to do that (short of just a master while-loop) depends on what you're trying to do and if you can use some frameworks/what OS you're on etc... Commented Jan 4, 2014 at 14:46
  • 1
    How does that function ever even reach the recursive call? That loop shouldn't ever stop. Even if cur1 weren't a different type from cur2, it would never go from not being equal to suddenly being equal. You'd just keep setting flag = 0 forever. Commented Jan 4, 2014 at 14:50

2 Answers 2

3

Regarding the first function - just put everything inside a while loop:

def startEnd():
    while True:
        flag = 0
        f = open('file')
        lq = f.readlines()
        cur2 = lq[0]
        cur1 = datetime.datetime.now()
        while flag == 0:
            if cur1 == cur2: # cur2 is datetime read from file
                q.put(True)
                flag = 1
            else:
                flag = 0
Sign up to request clarification or add additional context in comments.

Comments

2

Note that your second function is not recursive - it will never trigger a recursion depth error. Yes, planIncr is passed to threading.Timer, but not executed by that call. planIncr is executed by some other thread later, and the thread that called planIncr to begin with sees planIncr return right away.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.