Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the Python retry mechanism

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is the Python retry mechanism". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the Python retry mechanism".

In order to avoid functional problems caused by some network or other uncontrollable factors. For example, when sending a request, there is often the problem of request timeout because of the instability of the network.

In this case, we usually add retried code to the code. The retried code itself is not difficult to implement, but how to write elegantly and easy to use is a problem we have to consider.

What we want to introduce here is a third-party library-Tenacity (the retry mechanism in the title is not accurate, it is not a built-in module of Python, so it cannot be called a mechanism), it implements almost all retry scenarios that we can use, such as:

Under what circumstances do I retry?

How many times should I try again?

How long will it take to finish the retry?

How long is the interval between each retry?

Callback after a failed retry?

Install it before you can use it

$pip install tenacity

1. The most basic retry

Unconditional retry with no interval between retries

From tenacity import retry @ retry def test_retry (): print ("wait for retry, retry without interval.") Raise Exception test_retry ()

Retry unconditionally, but wait 2 seconds before retrying

From tenacity import retry, wait_fixed @ retry (wait=wait_fixed (2)) def test_retry (): print ("waiting for retry...") Raise Exception test_retry ()

two。 Set the basic conditions for stopping

Only retry 7 times.

From tenacity import retry, stop_after_attempt @ retry (stop=stop_after_attempt (7)) def test_retry (): print ("waiting for retry...") Raise Exception test_retry ()

Try again for 10 seconds and do not try again

From tenacity import retry, stop_after_delay @ retry (stop=stop_after_delay (10)) def test_retry (): print ("waiting for retry...") Raise Exception test_retry ()

Or if one of the above two conditions is met, the retry is over.

From tenacity import retry, stop_after_delay, stop_after_attempt @ retry (stop= (stop_after_delay (10) | stop_after_attempt (7)) def test_retry (): print ("waiting for retry.") Raise Exception test_retry ()

3. Set when to retry

In the event of a specific error / exception (such as request timeout), try again

From requests import exceptions from tenacity import retry, retry_if_exception_type @ retry (retry=retry_if_exception_type (exceptions.Timeout)) def test_retry (): print ("waiting for retry...") Raise exceptions.Timeout test_retry ()

Try again when the custom condition is met.

The following example retries when the return value of the test_retry function is False

From tenacity import retry, stop_after_attempt, retry_if_result def is_false (value): return value is False @ retry (stop=stop_after_attempt (3), retry=retry_if_result (is_false) def test_retry (): return False test_retry ()

4. Error is thrown again after retry

When an exception occurs, tenacity will retry. If the retry still fails, by default, the exception thrown up will become RetryError rather than the root cause.

So you can add a parameter (reraise=True) so that if the retry fails, the exception thrown will be the same.

From tenacity import retry, stop_after_attempt @ retry (stop=stop_after_attempt (7), reraise=True) def test_retry (): print ("waiting for retry...") Raise Exception test_retry ()

5. Set callback function

When the last retry fails, you can execute a callback function

From tenacity import * def return_last_value (retry_state): print ("execute callback function") return retry_state.outcome.result () # indicates the return value of the original function def is_false (value): return value is False @ retry (stop=stop_after_attempt (3), retry_error_callback=return_last_value Retry=retry_if_result (is_false)) def test_retry (): print ("waiting for retry...") Return False print (test_retry ())

The output is as follows

Waiting for a retry. Waiting for a retry. Waiting for a retry. Implement the callback function False. Thank you for your reading. The above is the content of "what is the Python retry mechanism". After the study of this article, I believe you have a deeper understanding of what the Python retry mechanism is, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report