← 返回题库
中级

实现指数退避重试策略

未完成
中级参考 代码结构已给出,请填写 ____ 处
def solve():
    from pyodide.http import open_url
    from io import StringIO
    circuit_breaker_log_csv = open_url(____).read()
    import random
    random.seed(____)
    def exponential_backoff(____):
        delay = min(base * (____ ** attempt), cap)
        if jitter: delay = delay * random.uniform(____, ____)
        return round(____)
    def retry_with_backoff(____):
        for attempt in range(____):
            result = '____' if attempt >= success_on else '____'
            if result == '____':
                print(____)
                return True
            delay = exponential_backoff(____)
            print(____)
        print(____)
        return False
    print(____)
    retry_with_backoff(____)
    print(____)
    retry_with_backoff(____)

示例

输入
solve()
期望输出
重试场景1 (success_on=2):
  attempt=0: FAIL, next retry in 1.139s
  attempt=1: FAIL, next retry in 1.05s
  attempt=2: SUCCESS
重试场景2 (success_on=6, should fail):
  attempt=0: FAIL, next retry in 0.775s
  attempt=1: FAIL, next retry in 1.446s
  attempt=2: FAIL, next retry in 4.946s
  attempt=3: FAIL, next retry in 9.414s
  attempt=4: FAIL, next retry in 22.275s
  all 4 retries exhausted
Python 代码 🔒 登录后使用
🔒

登录后即可练习

注册免费账号,在浏览器中直接运行 Python 代码