urllib3 中重试之间的静态时间

问题描述 投票:0回答:1

我想在重试之间使用静态时间,而不是 urllib3 Retry 类中的指数退避因子。

我尝试包装 Retry 类来实现静态重试。然而,虽然

LoggingRetry
正在使用适当的参数进行实例化,但在实际重试发生时将使用默认值。在下面的示例中,重试之间应该有两秒钟的时间。
backoff_factor
会持续存在,但我似乎无法让
delay_between_retries
坚持下去。

import time
import urllib3
from urllib3.util.retry import Retry
from urllib3.exceptions import MaxRetryError
from requests. exceptions import HTTPError
import traceback



# Define the retry strategy with logging
class LoggingRetry(Retry):
    def __init__(self, delay_between_retries=0, *args, **kwargs):
        print(f"Initializing LoggingRetry with delay_between_retries={delay_between_retries}")
        self.delay_between_retries = delay_between_retries
        super().__init__(*args, **kwargs)

    def sleep(self, response=None):
        backoff = self.get_backoff_time()
        total_sleep_time = self.delay_between_retries + backoff
        print(f"LoggingRetry sleep -  delay_between_retries: {self.delay_between_retries}, Backoff time: {backoff}")

        time.sleep(total_sleep_time)


class MyClient:
    def __init__(self, total_tries=3, delay_between_retries=0, backoff_factor=0):
        self.total_tries = total_tries
        self.delay_between_retries = delay_between_retries
        self.backoff_factor = backoff_factor

        # Define a retry strategy with a custom fixed_wait_time
        self.retry_strategy = LoggingRetry(
            total=self.total_tries,
            status_forcelist=[202],
            backoff_factor=self.backoff_factor,
            delay_between_retries=self.delay_between_retries,
            raise_on_status=False,
            allowed_methods=frozenset(['GET'])
        )
        
        # Initialize HTTP client with retry strategy
        self.http = urllib3.PoolManager(retries=self.retry_strategy, maxsize=4, block=True)

    
    def request_data(self, url):    
        try:
            response = self.http.request('GET', url,)
            return response
        except MaxRetryError as me:
            print(f"Max retries reached: {me}")
        except HTTPError as he:
            print(f"An HTTP error occurred: {he}")
            traceback.print_exc()
        except Exception as e:
            print(f"An error occurred: {e}")
            traceback.print_exc()

MyClient(delay_between_retries=2, backoff_factor=0).request_data(url='http://0.0.0.202/')
python urllib3
1个回答
0
投票

在你的子类中实现另一个方法:

class LoggingRetry(Retry):
    ...

    def new(self, *args, **kwargs):
        return super().new(
           *args,
           **kwargs,
           delay_between_retries=self.delay_between_retries,
        )

说明:

urllib3.Retry
的工作方式是,每次重试尝试都会创建一个新的重试实例,并增加重试计数器。如果您需要更多控制,您也可以覆盖
Retry.increment

© www.soinside.com 2019 - 2024. All rights reserved.