安装 pip install retry
Retry装饰器

retry(exceptions=Exception, tries=-1, delay=0, max_delay=None, backoff=1, jitter=0, logger=logging_logger):

"""Return a retry decorator.

 

    :param exceptions: an exception or a tuple of exceptions to catch. default: Exception.

    :param tries: the maximum number of attempts. default: -1 (infinite).

    :param delay: initial delay between attempts. default: 0.

    :param max_delay: the maximum value of delay. default: None (no limit).

    :param backoff: multiplier applied to delay between attempts. default: 1 (no backoff).

    :param jitter: extra seconds added to delay between attempts. default: 0.

                   fixed if a number, random if a range tuple (min, max)

    :param logger: logger.warning(fmt, error, delay) will be called on failed attempts.

                   default: retry.logging_logger. if None, logging is disabled.

    """

使用

@retry(ZeroDivisionError, tries=3, delay=2)

def make_trouble():

 
retry_call

def retry_call(f, fargs=None, fkwargs=None, exceptions=Exception, tries=-1, delay=0, max_delay=None, backoff=1,

jitter=0,

logger=logging_logger):

"""

    Calls a function and re-executes it if it failed.

 

    :param f: the function to execute.

    :param fargs: the positional arguments of the function to execute.

    :param fkwargs: the named arguments of the function to execute.

    :param exceptions: an exception or a tuple of exceptions to catch. default: Exception.

    :param tries: the maximum number of attempts. default: -1 (infinite).

    :param delay: initial delay between attempts. default: 0.

    :param max_delay: the maximum value of delay. default: None (no limit).

    :param backoff: multiplier applied to delay between attempts. default: 1 (no backoff).

    :param jitter: extra seconds added to delay between attempts. default: 0.

                   fixed if a number, random if a range tuple (min, max)

    :param logger: logger.warning(fmt, error, delay) will be called on failed attempts.

                   default: retry.logging_logger. if None, logging is disabled.

    :returns: the result of the f function.

    """

该方法和retry装饰器类似,除了它带函数和函数的参数,他可以动态的判断重试次数

import requests

from retry.api import retry_call

def make_trouble(service, info=None):

if not info:

info = ''

r = requests.get(service + info)

return r.text

def what_is_my_ip(approach=None):

if approach == "optimistic":

tries = 1

elif approach == "conservative":

tries = 3

else:

# skeptical

tries = -1

result = retry_call(make_trouble, fargs=["http://ipinfo.io/"], fkwargs={"info": "ip"}, tries=tries)

print(result)

what_is_my_ip("conservative")

 

retry之python重试机制的更多相关文章

  1. Java之Retry重试机制详解

    应用中需要实现一个功能: 需要将数据上传到远程存储服务,同时在返回处理成功情况下做其他操作.这个功能不复杂,分为两个步骤:第一步调用远程的Rest服务上传数据后对返回的结果进行处理:第二步拿到第一步结 ...

  2. springboot 整合retry(重试机制)

    当我们调用一个接口可能由于网络等原因造成第一次失败,再去尝试就成功了,这就是重试机制,spring支持重试机制,并且在Spring Cloud中可以与Hystaix结合使用,可以避免访问到已经不正常的 ...

  3. 【Dubbo 源码解析】07_Dubbo 重试机制

    Dubbo 重试机制 通过前面 Dubbo 服务发现&引用 的分析,我们知道,Dubbo 的重试机制是通过 com.alibaba.dubbo.rpc.cluster.support.Fail ...

  4. Spring Cloud 请求重试机制核心代码分析

    场景 发布微服务的操作一般都是打完新代码的包,kill掉在跑的应用,替换新的包,启动. spring cloud 中使用eureka为注册中心,它是允许服务列表数据的延迟性的,就是说即使应用已经不在服 ...

  5. Volley超时重试机制

    基础用法 Volley为开发者提供了可配置的超时重试机制,我们在使用时只需要为我们的Request设置自定义的RetryPolicy即可. 参考设置代码如下: int DEFAULT_TIMEOUT_ ...

  6. SpringCloud | FeignClient和Ribbon重试机制区别与联系

    在spring cloud体系项目中,引入的重试机制保证了高可用的同时,也会带来一些其它的问题,如幂等操作或一些没必要的重试. 今天就来分别分析一下 FeignClient 和 Ribbon 重试机制 ...

  7. Spring Cloud重试机制与各组件的重试总结

    SpringCloud重试机制配置 首先声明一点,这里的重试并不是报错以后的重试,而是负载均衡客户端发现远程请求实例不可到达后,去重试其他实例. ? 1 2 3 4 5 6 7 8 @Bean @Lo ...

  8. spring-retry 重试机制

    业务场景 应用中需要实现一个功能: 需要将数据上传到远程存储服务,同时在返回处理成功情况下做其他操作.这个功能不复杂,分为两个步骤:第一步调用远程的Rest服务逻辑包装给处理方法返回处理结果:第二步拿 ...

  9. guava的重试机制guava-retrying使用

    1,添加maven依赖 <dependency> <groupId>com.github.rholder</groupId> <artifactId>g ...

随机推荐

  1. HanLP分词命名实体提取详解

    HanLP分词命名实体提取详解   分享一篇大神的关于hanlp分词命名实体提取的经验文章,文章中分享的内容略有一段时间(使用的hanlp版本比较老),最新一版的hanlp已经出来了,也可以去看看新版 ...

  2. mysql的变量信息详解

    mysql的变量详解 执行show variables命令可以查看MySQL服务器的变量 变量名 默认值 说明 对应的配置文件参数 auto_increment_increment 1 自增长类型的初 ...

  3. enc28J60 网页控制LED灯

    软件IDE:Arduino 1.6.3 1.库的安装: 从https://github.com/jcw/ethercard 下载源码包,解压,复制ethercard-master文件夹到Arduino ...

  4. WPF Demo13 GridSplitter

    <Window x:Class="Commands.MainWindow" xmlns="http://schemas.microsoft.com/winfx/20 ...

  5. SQLServer数据库自增长标识列的更新修改操作

    SQLServer数据库自增长标识列的更新修改操作方法在日常的sql server开发中,经常会用到Identity类型的标识列作为一个表结构的自增长编号.比如文章编号.记录序号等等.自增长的标识列的 ...

  6. Dubbo的原理以及详细原理、配置

    Dubbo的背景 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进. Dubbo的应用 用于大规模 ...

  7. 玄学曲线并不玄 教你如何看懂GPU呈现

    红色代表了“执行时间”,它指的是Android渲染引擎执行盒子中这些绘制命令的时间,假如当前界面的视图越多,那么红色便会“跳”得越高.实际使用中,比如我们平时刷淘宝App时遇到出现多张缩略图需要加载时 ...

  8. PyQt5显示一个空白的窗口

    效果如下图: """ In this example, we create a simple window in PyQt5. """ # ...

  9. 将字符串表示的IP地址转变为整形表示

    当时面试上机的想法是,直接使用uint32_t变量来存ip地址,遍历字符串带".",然后去值,利用移位来将这个值填到uint32_t对应的位置上.这样的麻烦之处在于: 1,遍历字符 ...

  10. JAVA代码模板总结

    静态工厂方法+服务提供者框架模板 构造器模板 事件通知模板 单元素枚举类型singleton模块 私有构造器不可实例化类模板