审查生成的重试处理器

来自 闭包
Python 3.14 进阶 8分钟 找出 3处问题

请在任务运行器使用这段生成的代码前进行审查。

为每个任务创建一个重试处理器,独立记录尝试次数,执行 max_attempts 限制,并提供只读失败记录。

Python
def build_retry_handlers(jobs, max_attempts):
    handlers = []
    failures = []

    for job in jobs:
        attempts = 0

        def retry():
            nonlocal attempts, failures
            if attempts > max_attempts:
                raise RuntimeError("retry limit reached")

            attempts += 1
            try:
                return job.run(attempts)
            except Exception as error:
                failures = failures + [(job.name, str(error))]
                raise

        handlers.append(retry)

    def failure_history():
        return tuple(failures)

    return handlers, failure_history

生成代码仅作示例,不代表任何特定模型

在试验场中打开
报告错误