samedi 27 février 2021

How to retry x times and put a long sleep with a print

I have been trying to understand how backoff works. My goal is that whenever I reach a status_code etc: 405 5 times. I want to put a sleep of 60000 sec and print out that there has occured status error 405.

Right now I have written:

import time

import backoff
import requests

@backoff.on_exception(
    backoff.expo,
    requests.exceptions.RequestException,
    max_tries=5,
    giveup=lambda e: e.response is not None and e.response.status_code == 405
)
def publish(url):
    r = requests.post(url, timeout=10)
    r.raise_for_status()


publish("https://www.google.se/")

and what happens now is that if it just reaches 405 once, it will raise the status_code and stop the script. What im looking for is how I can make the script to retry 5 times, if the status is 5 times in a row of 405, then we want to put a long sleep and print it out. How can I do that using backofF? Im also up for other suggestions :)

Aucun commentaire:

Enregistrer un commentaire