码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2020-06-02 10:55:59      阅读:89      评论:0      收藏:0      [点我收藏+]

标签: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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!