python http requests失败重试方法, 退避算法(Retrying HTTP Requests with Backoff)

2020-12-28 04:27

阅读:478

标签:dom   red   print   rac   turn   starting   __name__   ret   name   

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

python http requests失败重试方法, 退避算法(Retrying HTTP Requests with Backoff)

标签:dom   red   print   rac   turn   starting   __name__   ret   name   

原文地址:https://www.cnblogs.com/jeff-ideas/p/13029672.html


评论


亲,登录后才可以留言!