4th Street Bar Hive-Bar

Hive-Bar Powered by Hive beta-fdb5b5b

Community post

introducemyself

introducemyself

![](https://cdn.steemitimages.com/DQmWsAQEx76MZLbaq44n6zCAN5PwSVMCXjYwRv1rfcrmg7a/image.png)

Hi Steem! My name is ool. This is my first contact with this platform.
I know that there are many people who know technology.So I came here!
Share a great tool today, based on the python!

**Tenacity**

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.

简单的来说 就是一个装饰器来完成处理异常并重试的功能

举个简单的例子如下:

import random
from tenacity import retry

@retry
def do_something_unreliable():
    a = random.randint(0, 10)
    print(a)
    if a > 5:
        raise IOError("Broken sauce, everything is hosed!!!111one")
    else:
        return "Awesome sauce!"

print(do_something_unreliable())

执行结果如下:

9
8
4
Awesome sauce!

上面的意思,就是 ,当随机的值大于5的时候发生异常,会继续执行这个函数,直到小于等于5的时候得到一个返回值。

有很多用途,比如写爬虫的时候,出现异常就会断掉 ,对程序的影响很大。有了这个类库就简单的多了

25 upvotes $0.02

Replies (27)

Review before signing

Posting as . Signing with . Keychain permission: Posting. Hive Keychain will ask you to approve this action next.


  
Technical details

Operation fingerprint: