Home > database >  Error trying to add 1 second of sleep for a retry
Error trying to add 1 second of sleep for a retry

Time:01-28

In this example I try to activate main() again in case there is an error in one():

from random import randrange
import sys
import time

def one():
    number = randrange(2)
    1/number

def main():
    try:
        one()
    except:
        sleep_time = 0
        while True:
            print('Error')
            sleep_time  = 1
            print('Next attempt in: '  str(sleep_time)   ' second(s)')
            time.sleep(sleep_time)
            main()
            sys.exit(1)

    print('Continue')

main()

When two errors occur in a row, it keeps giving a 1 second pause:

Error
Next attempt in: 1 second(s)
Error
Next attempt in: 1 second(s)
Continue

What I'm actually looking for would be an answer like this:

Error
Next attempt in: 1 second(s)
Error
Next attempt in: 2 second(s)
Continue

How should I proceed?

CodePudding user response:

You are overwriting sleep_time back to zero with every recursive call to main Assign it as a param of the function

def main(sleep_time=0):
    try:
        one()
    except:
        while True:
            print('Error')
            sleep_time  = 1
            print('Next attempt in: '  str(sleep_time)   ' second(s)')
            time.sleep(sleep_time)
            main(sleep_time=sleep_time)
            sys.exit(1)

    print('Continue')

out

Error
Next attempt in: 1 second(s)
Error
Next attempt in: 2 second(s)
Error
Next attempt in: 3 second(s)

CodePudding user response:

The problem was also identified in the other answers. An alternative solution, probably less "beautiful" than Frederick's is to pass the variable sleep_time as input to the main function:

from random import randrange
import sys
import time

def one():
    number = randrange(2)
    1/number

def main(sleep_time):
    try:
        one()
    except:
        
        while True:
            print('Error')
            sleep_time  = 1
            print('Next attempt in: '  str(sleep_time)   ' second(s)')
            time.sleep(sleep_time)
            main(sleep_time)
            sys.exit(1)

    print('Continue')

main(0)

CodePudding user response:

The problem is, that you always call the main function again and reset sleep_time to 0 and then add 1 again. Its working without calling it again:

from random import randrange
import sys
import time

def one():
    number = randrange(2)
    1/number

def main():
    try:
        one()
    except:
        sleep_time = 0
        while True:
            print('Error')
            sleep_time  = 1
            print('Next attempt in: '  str(sleep_time)   ' second(s)')
            time.sleep(sleep_time)

    print('Continue')

main()

CodePudding user response:

Using try inside while would make your code way easier to read and probably eliminates to need to call main() over and over again:

import sys
import time


def one():
    raise ValueError("Error!")


def main():
    sleep_time = 0
    while 1:
        try:
            one()
        except:
            sleep_time  = 1
            print(f'Next attempt in: {sleep_time} second(s)')
            time.sleep(sleep_time)
        else:
            print("Ok. Exit here")
            sys.exit(0)

main()

Out:

Next attempt in: 1 second(s)
Next attempt in: 2 second(s)
Next attempt in: 3 second(s)
...
  •  Tags:  
  • Related