Loading

python函数装饰器,返回自定义类型

from typing import Any, Callable, TypeVar
import requests
T = TypeVar("T")

class MaxRetryError(Exception):
    def __init__(self):
        message = "超出最大重试次数"
        super().__init__(message)

class MaxRetry:
    def __init__(self, max_retry: int = 2):
        self.max_retry = max_retry

    def __call__(self, connect_once: Callable[..., T]) -> Callable[..., T]:
        def connect_n_times(*args: Any, **kwargs: Any) -> T:
            retry = self.max_retry + 1
            while retry:
                try:
                    return connect_once(*args, **kwargs)
                except requests.exceptions.Timeout as e:
                    print("抓取超时,正在尝试重新连接~")
                finally:
                    retry -= 1
            raise MaxRetryError()

        return connect_n_times
@MaxRetry(2)
def get_info(url: str) -> str:
    re = requests.get(url, timeout=2)
    return re.text
url = "https://www.abc.com/4de25977c7dce0"
get_info(url)

[output]:
抓取超时,正在尝试重新连接~
抓取超时,正在尝试重新连接~
抓取超时,正在尝试重新连接~
---------------------------------------------------------------------------
MaxRetryError                             Traceback (most recent call last)
<ipython-input-37-bc2dde821bd1> in <module>
----> 1 get_info(url)

<ipython-input-34-b40f19f5bf7c> in connect_n_times(*args, **kwargs)
     22                 finally:
     23                     retry -= 1
---> 24             raise MaxRetryError()
     25 
     26         return connect_n_times

MaxRetryError: 超出最大重试次数
posted @ 2021-06-23 10:53  JinX-Digital  阅读(186)  评论(0)    收藏  举报