少有人知的 Python "重试机制"

开发 后端
为了避免由于一些网络或等其他不可控因素,而引起的功能性问题。比如在发送请求时,会因为网络不稳定,往往会有请求超时的问题。

 为了避免由于一些网络或等其他不可控因素,而引起的功能性问题。比如在发送请求时,会因为网络不稳定,往往会有请求超时的问题。

[[334139]]

这种情况下,我们通常会在代码中加入重试的代码。重试的代码本身不难实现,但如何写得优雅、易用,是我们要考虑的问题。

这里要给大家介绍的是一个第三方库 - Tenacity (标题中的重试机制并并不准确,它不是 Python 的内置模块,因此并不能称之为机制),它实现了几乎我们可以使用到的所有重试场景,比如:

  1. 在什么情况下才进行重试?
  2. 重试几次呢?
  3. 重试多久后结束?
  4. 每次重试的间隔多长呢?
  5. 重试失败后的回调?

在使用它之前 ,先要安装它

 

  1. $ pip install tenacity 

1. 最基本的重试

无条件重试,重试之间无间隔

 

  1. from tenacity import retry 
  2.  
  3. @retry 
  4. def test_retry(): 
  5.     print("等待重试,重试无间隔执行..."
  6.     raise Exception 
  7.  
  8. test_retry() 

无条件重试,但是在重试之前要等待 2 秒

 

  1. from tenacity import retry, wait_fixed 
  2.  
  3. @retry(wait=wait_fixed(2)) 
  4. def test_retry(): 
  5.     print("等待重试..."
  6.     raise Exception 
  7.  
  8. test_retry() 

2. 设置停止基本条件

只重试7 次

 

  1. from tenacity import retry, stop_after_attempt 
  2.  
  3. @retry(stop=stop_after_attempt(7)) 
  4. def test_retry(): 
  5.     print("等待重试..."
  6.     raise Exception 
  7.  
  8. test_retry() 

重试 10 秒后不再重试

 

  1. from tenacity import retry, stop_after_delay 
  2.  
  3. @retry(stop=stop_after_delay(10)) 
  4. def test_retry(): 
  5.     print("等待重试..."
  6.     raise Exception 
  7.  
  8. test_retry() 

或者上面两个条件满足一个就结束重试

 

  1. from tenacity import retry, stop_after_delay, stop_after_attempt 
  2.  
  3. @retry(stop=(stop_after_delay(10) | stop_after_attempt(7))) 
  4. def test_retry(): 
  5.     print("等待重试..."
  6.     raise Exception 
  7.  
  8. test_retry() 

3. 设置何时进行重试

在出现特定错误/异常(比如请求超时)的情况下,再进行重试

 

  1. from requests import exceptions 
  2. from tenacity import retry, retry_if_exception_type 
  3.  
  4. @retry(retry=retry_if_exception_type(exceptions.Timeout)) 
  5. def test_retry(): 
  6.     print("等待重试..."
  7.     raise exceptions.Timeout 
  8.  
  9. test_retry() 

在满足自定义条件时,再进行重试。

如下示例,当 test_retry 函数返回值为 False 时,再进行重试

 

  1. from tenacity import retry, stop_after_attempt, retry_if_result 
  2.  
  3. def is_false(value): 
  4.     return value is False 
  5.  
  6. @retry(stop=stop_after_attempt(3), 
  7.        retry=retry_if_result(is_false)) 
  8. def test_retry(): 
  9.     return False 
  10.  
  11. test_retry() 

4. 重试后错误重新抛出

当出现异常后,tenacity 会进行重试,若重试后还是失败,默认情况下,往上抛出的异常会变成 RetryError,而不是最根本的原因。

因此可以加一个参数(reraise=True),使得当重试失败后,往外抛出的异常还是原来的那个。

 

  1. from tenacity import retry, stop_after_attempt 
  2.  
  3. @retry(stop=stop_after_attempt(7), reraise=True
  4. def test_retry(): 
  5.     print("等待重试..."
  6.     raise Exception 
  7.  
  8. test_retry() 

5. 设置回调函数

当最后一次重试失败后,可以执行一个回调函数

 

  1. from tenacity import * 
  2.  
  3. def return_last_value(retry_state): 
  4.     print("执行回调函数"
  5.     return retry_state.outcome.result()  # 表示返回原函数的返回值 
  6.  
  7. def is_false(value): 
  8.     return value is False 
  9.  
  10. @retry(stop=stop_after_attempt(3), 
  11.        retry_error_callback=return_last_value, 
  12.        retry=retry_if_result(is_false)) 
  13. def test_retry(): 
  14.     print("等待重试中..."
  15.     return False 
  16.  
  17. print(test_retry()) 

输出如下

 

  1. 等待重试中... 
  2. 等待重试中... 
  3. 等待重试中... 
  4. 执行回调函数 
  5. False 

 

责任编辑:华轩 来源: Python编程时光
相关推荐

2019-03-19 09:00:14

Python 开发编程语言

2022-11-14 08:19:59

重试机制Kafka

2021-02-20 10:02:22

Spring重试机制Java

2022-05-06 07:44:10

微服务系统设计重试机制

2017-07-02 16:50:21

2023-10-27 08:20:12

springboot微服务

2017-06-16 15:16:15

2023-11-27 07:44:59

RabbitMQ机制

2023-11-06 08:00:38

接口高可用机制

2024-01-04 18:01:55

高并发SpringBoot

2018-06-04 22:27:47

2021-04-09 23:11:25

Python程序开源

2023-11-17 11:55:54

Pythonretrying库

2019-10-30 12:24:57

网络安全安全风险网络攻击

2021-02-04 13:32:55

jsp技术servlet

2021-12-08 08:30:55

Java AQS机制 Java 基础

2020-08-24 07:34:39

网络超时请求

2023-10-17 08:01:46

MQ消息重试

2022-03-28 08:36:15

tenacityPython

2019-12-11 16:46:40

技术创业人工智能
点赞
收藏

51CTO技术栈公众号