Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

If return error, try again in 1 minute, if another return error occurs, try again in 2 minutes, and so on

To try again in 1 minute, I use sleep(60):

import requests
from time import sleep

headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36"
    }

def part_1():
    url = f'https://errorerrorerror.com'
    response = requests.get(url, headers=headers, timeout=1).json()
    return response

def main():
    print('Start Run')
    try:
        part_1()
    except:
        sleep(60)
        main()

if __name__ == '__main__':
    main()

So that I can try again when there is an error, I add in except the call again to main().

But I would like to add 1 more minute to each attempt, for example:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

There was an error: try again in 1 minute
There was another error: try again in 2 minutes
There was another error: try again in 3 minutes

And so on. How should I proceed to make this possible?

>Solution :

Avoid recursion in a case like this where there is no upper bound to the amount of times the function will recurse, as you will eventually get a Stack Overflow Error

def main():
    sleep_time = 60
    while True:
        print('Start Run')
        try:
            part_1()
            break
        except:
            sleep(sleep_time)
            sleep_time += 60

if __name__ == '__main__':
    main()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading