程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python retrying easy to use instructions

編輯:Python

So let's just set it up retrying modular :

pip install retrying -i https://pypi.tuna.tsinghua.edu.cn/simple

Decorate before the function after importing the package :

from retrying import retry
import random
# I'm going to randomly generate a number , The required return is a value less than 1 The number of
@retry(stop_max_attempt_number=100) #stop_max_attempt_number Represents the maximum number of retries
def fun():
result = random.randint(0, 10)
print(' Current number size :', result)
if result <= 1:
print('*** The current number is less than 1, Can return ***')
return result
else:
raise ValueError
fun()

Output results :

Current number size : 3
Current number size : 2
Current number size : 4
Current number size : 5
Current number size : 7
Current number size : 8
Current number size : 10
Current number size : 4
Current number size : 0
*** The current number is less than 1, Can return ***

Code :

from retrying import retry
import random

# I'm going to randomly generate a number , The required return is a value less than 1 The number of
@retry(stop_max_attempt_number=100)  #stop_max_attempt_number Represents the maximum number of retries
def fun():
    result = random.randint(0, 10)
    print(' Current number size :', result)
    if result <= 1:
        print('*** The current number is less than 1, Can return ***')
        return result
    else:
        raise ValueError

    
fun()


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved