自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

少有人知的 Python "重試機制"

開發(fā) 后端
為了避免由于一些網(wǎng)絡(luò)或等其他不可控因素,而引起的功能性問題。比如在發(fā)送請求時,會因為網(wǎng)絡(luò)不穩(wěn)定,往往會有請求超時的問題。

 為了避免由于一些網(wǎng)絡(luò)或等其他不可控因素,而引起的功能性問題。比如在發(fā)送請求時,會因為網(wǎng)絡(luò)不穩(wěn)定,往往會有請求超時的問題。

[[334139]]

這種情況下,我們通常會在代碼中加入重試的代碼。重試的代碼本身不難實現(xiàn),但如何寫得優(yōu)雅、易用,是我們要考慮的問題。

這里要給大家介紹的是一個第三方庫 - Tenacity (標(biāo)題中的重試機制并并不準(zhǔn)確,它不是 Python 的內(nèi)置模塊,因此并不能稱之為機制),它實現(xiàn)了幾乎我們可以使用到的所有重試場景,比如:

  1. 在什么情況下才進(jìn)行重試?
  2. 重試幾次呢?
  3. 重試多久后結(jié)束?
  4. 每次重試的間隔多長呢?
  5. 重試失敗后的回調(diào)?

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

 

  1. $ pip install tenacity 

1. 最基本的重試

無條件重試,重試之間無間隔

 

  1. from tenacity import retry 
  2.  
  3. @retry 
  4. def test_retry(): 
  5.     print("等待重試,重試無間隔執(zhí)行..."
  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. 設(shè)置停止基本條件

只重試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() 

或者上面兩個條件滿足一個就結(jié)束重試

 

  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. 設(shè)置何時進(jìn)行重試

在出現(xiàn)特定錯誤/異常(比如請求超時)的情況下,再進(jìn)行重試

 

  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() 

在滿足自定義條件時,再進(jìn)行重試。

如下示例,當(dāng) test_retry 函數(shù)返回值為 False 時,再進(jìn)行重試

 

  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. 重試后錯誤重新拋出

當(dāng)出現(xiàn)異常后,tenacity 會進(jìn)行重試,若重試后還是失敗,默認(rèn)情況下,往上拋出的異常會變成 RetryError,而不是最根本的原因。

因此可以加一個參數(shù)(reraise=True),使得當(dāng)重試失敗后,往外拋出的異常還是原來的那個。

 

  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. 設(shè)置回調(diào)函數(shù)

當(dāng)最后一次重試失敗后,可以執(zhí)行一個回調(diào)函數(shù)

 

  1. from tenacity import * 
  2.  
  3. def return_last_value(retry_state): 
  4.     print("執(zhí)行回調(diào)函數(shù)"
  5.     return retry_state.outcome.result()  # 表示返回原函數(shù)的返回值 
  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. 執(zhí)行回調(diào)函數(shù) 
  5. False 

 

責(zé)任編輯:華軒 來源: Python編程時光
相關(guān)推薦

2019-03-19 09:00:14

Python 開發(fā)編程語言

2022-11-14 08:19:59

重試機制Kafka

2024-09-25 08:32:05

2025-02-26 10:49:14

2021-02-20 10:02:22

Spring重試機制Java

2022-05-06 07:44:10

微服務(wù)系統(tǒng)設(shè)計重試機制

2023-10-27 08:20:12

springboot微服務(wù)

2025-01-03 08:44:37

kafka消息發(fā)送策略

2017-07-02 16:50:21

2017-06-16 15:16:15

2023-11-27 07:44:59

RabbitMQ機制

2023-11-06 08:00:38

接口高可用機制

2018-06-04 22:27:47

2025-04-18 03:00:00

2024-01-04 18:01:55

高并發(fā)SpringBoot

2025-02-27 09:35:22

2024-09-30 08:30:37

2025-02-26 08:10:40

2019-10-30 12:24:57

網(wǎng)絡(luò)安全安全風(fēng)險網(wǎng)絡(luò)攻擊

2024-08-06 09:51:21

SpringHTTPJSON
點贊
收藏

51CTO技術(shù)棧公眾號