Source: https://stackoverflow.com/a/35504626
It will retry at intervals of the number of seconds specified by backoff_factor x the number of retries. In the following cases, the first retry is 1 second and the second retry is 2 seconds. Retry when the status code specified in status_forcelist is returned or when it times out. The parameters that can be specified for Retry are described in here.
import requests
from requests.packages.urllib3.util.retry import Retry
from requests.adapters import HTTPAdapter
s = requests.Session()
retries = Retry(total=5,
backoff_factor=1,
status_forcelist=[ 500, 502, 503, 504 ])
s.mount('https://', HTTPAdapter(max_retries=retries))
s.mount('http://', HTTPAdapter(max_retries=retries))
r = s.request('GET', 'http://localhost:5000', timeout=2, headers={'Authorization': 'foobar'})
r.raise_for_status()
If you use a library called retry, you can go! I thought, but with this library, it seems that it is not possible to sort the processing by status code.
Recommended Posts