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:
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()