python http requests失败重试方法, 退避算法(Retrying HTTP Requests with Backoff)
import requests from datetime import datetime import time import random retry_timeout = 10 def http_request(url, first_request_time=None, retry_counter=0): """ first_request_time: The time of the first request (None if no retries have occurred). retry_counter: The number of this retry, or zero for first attempt. """ if not first_request_time: first_request_time = datetime.now() try: elapsed = datetime.now() - first_request_time if elapsed.total_seconds() > retry_timeout: raise TimeoutError if retry_counter > 0: # 0.5 * (1.5 ^ i) is an increased sleep time of 1.5x per iteration, # starting at 0.5s when retry_counter=0. The first retry will occur # at 1, so subtract that first. delay_seconds = 0.5 * 1.5**(retry_counter - 1) print(f"Delay {delay_seconds}") # Jitter this value by 50% and pause. time.sleep(delay_seconds * (random.random() + 0.5)) result = requests.get(url) return result.text except TimeoutError: print("Request Timed out") return None except requests.exceptions.ConnectionError: return http_request(url, first_request_time, retry_counter + 1) if __name__ == "__main__": response_text = http_request("https://thissitedoesntexist.com") # retries the request to an invalid url until the timeout of 10 second is reached # A TimeoutError is raised after 10 seconds
相关推荐
赶路人儿 2020-11-02
scuyxi 2020-10-25
csdnfelix 2020-10-18
代码之神 2020-10-15
Pokemogo 2020-09-17
MasterCui 2020-09-08
Morelia 2020-09-04
meylovezn 2020-08-30
Hannah 2020-08-19
hugebawu 2020-08-17
scuyxi 2020-08-16
hang0 2020-08-16
higher0 2020-08-11
Tips 2020-08-08
Tristahong 2020-08-05
xiesheng 2020-08-02
huakai 2020-07-26
RememberMePlease 2020-06-26
zangdaiyang 2020-06-25