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

posted @ 2020-06-02 10:23  Jeff_blog  阅读(967)  评论(0编辑  收藏  举报