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

Posted 日拱一卒

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

以上是关于python http requests失败重试方法, 退避算法(Retrying HTTP Requests with Backoff)的主要内容,如果未能解决你的问题,请参考以下文章

使用Python请求http/https时设置失败重试次数

在 vuejs 中重试失败的 ajax 请求

编写装饰器实现python请求错误重试功能

requests模块的请求失败自动重试设置和设置等待时间

python unittest 运行失败后重试

Restkit:重试失败的请求