zoukankan      html  css  js  c++  java
  • python的重试库tenacity用法以及类似库retry、requests实现

    介绍

    tenacity is an Apache 2.0 licensed general-purpose retrying library, written in Python, to simplify the task of adding retry behavior to just about anything. It originates from a fork of retrying which is sadly no longer maintained. Tenacity isn’t api compatible with retrying but adds significant new functionality and fixes a number of longstanding bugs.

    安装

    pip install tenacity

    文档:

    https://tenacity.readthedocs.io/en/latest/

    demo:

    In [13]: import random
        ...: from tenacity import retry
        ...:
        ...: @retry
        ...: def do_something_unreliable():
        ...:     value = random.randint(0,10)
        ...:     if value > 1:
        ...:         raise IOError("Broken sauce, everything is hosed!!!111one")
        ...:     else:
        ...:         print(value)
        ...:         return "Awesome sauce!"
        ...:
        ...: print(do_something_unreliable())
    1
    Awesome sauce!
    
    In [14]: import random
        ...: from tenacity import retry
        ...:
        ...: @retry
        ...: def do_something_unreliable():
        ...:     value = random.randint(0,10)
        ...:     if value > 1:
        ...:         raise IOError("Broken sauce, everything is hosed!!!111one")
        ...:     else:
        ...:         print(value)
        ...:         return "Awesome sauce!"
        ...:
        ...: print(do_something_unreliable())
    1
    Awesome sauce!
    
    In [15]: import random
        ...: from tenacity import retry
        ...:
        ...: @retry
        ...: def do_something_unreliable():
        ...:     value = random.randint(0,10)
        ...:     if value > 1:
        ...:         raise IOError("Broken sauce, everything is hosed!!!111one")
        ...:     else:
        ...:         print(value)
        ...:         return "Awesome sauce!"
        ...:
        ...: print(do_something_unreliable())
    0
    Awesome sauce!
    
    In [16]: import random
        ...: from tenacity import retry
        ...:
        ...: @retry
        ...: def do_something_unreliable():
        ...:     value = random.randint(0,10)
        ...:     if value > 1:
        ...:         raise IOError("Broken sauce, everything is hosed!!!111one")
        ...:     else:
        ...:         print(value)
        ...:         return "Awesome sauce!"
        ...:
        ...: print(do_something_unreliable())
    1
    Awesome sauce!

    源代码:

    https://github.com/jd/tenacity

    类似库:retry

    pip install retry

    自己可以参考retry源代码自己实现

    requests多次尝试实现:

    https://www.cnblogs.com/eshizhan/p/5072626.html

  • 相关阅读:
    P3916 图的遍历 题解
    NBL小可爱纪念赛「 第一弹 」 游记(部分题解)
    P4147 玉蟾宫 题解
    十、一些小例子
    九、基础正则表达式BRE
    八.linux系统文件属性知识
    七、linux目录结构知识---实战
    六、linux目录结构知识
    3.20-30岁形成好的习惯
    五、Centos linux系统优化-实战
  • 原文地址:https://www.cnblogs.com/shengulong/p/10373597.html
Copyright © 2011-2022 走看看