🦋
Butterfly 用户手册
  • Introduction
  • 一 前言
  • 二 开始
    • 安装部署
    • 五分钟体验指南
    • 单机使用手册
    • 应用规范
      • handler specs
      • middleware specs
      • xingqiao_plugin specs
      • yiqiu_program specs
  • 三 客户端功能
    • MySQL 原生协议
    • MySQL ORM
    • Redis 原生协议
      • redis_config
      • redis_tls
    • Redis ORM
    • Redis mcpack
    • Localcache
    • Kazoo
  • 四 应用(通用服务)
    • API JSON 规范
    • 异步任务 BaiChuan(百川)
    • 任务调度 RuQi(如期)
    • 任务编排 XingQiao(星桥)
    • 配置管理 WuXing(五行)
    • 运筹决策 BaiCe(百策)
  • 五 部署运维
    • 单机容器化部署
    • 监控
    • 异常排查
      • CPU Load spike every 7 hours
    • 升级
    • 安全
    • 其他
  • 六 前端
    • butterfly_template
    • butterfly_fe
    • butterfly-admin(json2web)
      • amis
      • sso
      • pangu
    • NoahV
    • PyWebIO
  • 七 潘多拉魔盒
    • 装饰器
      • localcache_decorator
      • retry_decorator
      • custom_decorator
      • command2http_decorator
    • 算法
      • 算法-分位数
      • 算法-变异系数
    • 实用工具
      • host_util
      • shell_util
      • http_util
      • time_util
      • random_util
      • concurrent
      • jsonschema
      • blinker
      • toml
      • command_util
      • config_util
      • picobox
      • 对称加密
        • des
        • aes
      • ascii_art
        • ttable
        • chart
      • business_rules
      • python-mysql-replication
      • dict_util
    • 中间件
      • middleware_status
      • middleware_whitelist
    • test_handler.py
  • 八 最佳实践
    • 分布式架构
    • Code practice
    • Log practice
    • Daemon process
  • 附录
Powered by GitBook
On this page
  • 1 功能
  • 2 例子
  • 2.1 简单例子
  • 2.2 装饰器
  • 2.3 添加超时时间
  • 2.4 添加 exceptions 参数
  1. 七 潘多拉魔盒
  2. 装饰器

retry_decorator

1 功能

  • 函数可以根据以下条件重试:

    • 执行结果是否成功 (like a status code not equaling 200)

    • Exceptions (like a function raising a requests.RequestException)

      • 所有接受的异常也会在失败时引发原始异常!

  • 函数执行失败可能是:

    • 超过最大重试次数

    • 达到最大超时时间

  • 函数重试可以按特定的间隔间隔进行

2 例子

2.1 简单例子

from xlib.util import retry

print "------------------------------------------func"
@retry.retry(max_retries=3, interval=5)
def add(a, b):
    return a + b


print add(3, 5)

print "------------------------------------------class"
class Demo():
    @retry.retry(max_retries=3, interval=5)
    def add(self, a, b):
        assert a == 4
        return a + b

demo = Demo()
print demo.add(3, 5)

2.2 装饰器

Send a GET request to a URL until it returns a status code of 200!

每秒重试一次

import requests

from xlib.util import retry

@retry.retry(max_retries=-1, interval=1, success=lambda x: x.status_code == 200)
def poll_url(url, method='GET'):
    return requests.request(method, url)

2.3 添加超时时间

Same function as above, timeout after 10 seconds!

import requests

from xlib.util import retry

@retry.retry(max_retries=-1, interval=1, success=lambda x: x.status_code == 200, timeout=10)
def poll_url(url, method='GET'):
    return requests.request(method, url)

2.4 添加 exceptions 参数

Same function as above, timeout after 10 seconds!

import requests

from xlib.util import retry

@retry.retry(
    exceptions=(requests.RequestException,), max_retries=-1, interval=1,
    success=lambda x: x.status_code == 200, timeout=10)
def poll_url(url, method='GET'):
    return requests.request(method, url)
Previouslocalcache_decoratorNextcustom_decorator

Last updated 1 year ago