# -*- coding: utf-8 -*- """工具函数""" import time from typing import Callable, Any def retry_on_exception(max_retries: int = 3, delay: float = 5.0) -> Callable: """异常重试装饰器""" def decorator(func: Callable) -> Callable: def wrapper(*args, **kwargs) -> Any: for attempt in range(max_retries): try: return func(*args, **kwargs) except Exception as e: if attempt == max_retries - 1: raise e print(f"尝试 {attempt + 1}/{max_retries} 失败: {e}") print(f"{delay}秒后重试...") time.sleep(delay) return None return wrapper return decorator