This article mainly introduces you to the solution to abnormal retry in Python. The introduction in the article is very detailed. I believe it has certain reference learning value for everyone to learn or use python. Friends who need it will follow the editor below. Let’s take a look.
Preface
When everyone is doing data capture, they often encounter program saving due to network problems. Previously, only errors were recorded. content, and post-process the error content.
Original process:
def crawl_page(url): pass def log_error(url): pass url = "" try: crawl_page(url) except: log_error(url)
Improved process:
attempts = 0 success = False while attempts < 3 and not success: try: crawl_page(url) success = True except: attempts += 1 if attempts == 3: break
Recently discovered new solution: retrying
retrying is a Python retry package , can be used to automatically retry some program segments that may fail. retrying
Provide a decorator function retry
. The decorated function will be re-executed if it fails. By default, it will continue to retry as long as an error is reported. .
import random from retrying import retry @retry def do_something_unreliable(): if random.randint(0, 10) > 1: raise IOError("Broken sauce, everything is hosed!!!111one") else: return "Awesome sauce!" print do_something_unreliable()
If we run the have_a_try
function, it will not end until random.randint
returns 5, otherwise it will Always re-execute.
retry can also accept some parameters. You can see the optional parameters from the initialization function of the Retrying class in the source code:
s<a href="http://www.php.cn/wiki/904.html" target="_blank">top</a>_max_attempt_number
: Used to set the maximum number of attempts. If the number is exceeded, retry will stop.
# #stop_max_delay: For example, set to 10000, then from the time when the decorated function starts executing to the time when the function ends successfully or fails with an error, as long as this period exceeds 10 seconds, the function will no longer Executed
wait_fixed: Set the dwell time between two
retrying
wait_random_min and wait_random_max: Randomly generate the residence time between two
retrying
: Generate the residence time between two retrying
in exponential form, the generated value is 2^previous_attempt_number * wait_exponential_multiplier
, previous_attempt_number
is the previous The number of retry
, if the generated value exceeds the size of wait_exponential_max
, then the stay value between the two retryings will be wait_exponential_max
. This design caters to the exponential backoff
algorithm, which can alleviate blocking situations.
exception<a href="http://www.php.cn/wiki/265.html" target="_blank"> to pass in a function</a> Object
:
def retry_if_io_error(exception): return isinstance(exception, IOError) @retry(retry_on_exception=retry_if_io_error) def read_a_file(): with open("file", "r") as f: return f.read()
function, if an exception is reported, then this exception will Pass the formal parameter exception
into the retry_if_io_error
function. If exception
is IOError
, then retry
will be performed. If Otherwise, it stops running and throws an exception
.
We can also specify which results to
when we get them. This requires using retry_on_result
to pass in a function object:
def retry_if_result_none(result): return result is None @retry(retry_on_result=retry_if_result_none) def get_result(): return None
is successful, the return value of the function
will be passed in in the form of formal parameter resultretry_if_result_none
In the function, if the return value is None
, then perform retry
, otherwise it will end and return the function value.
Summary
[Related recommendations]1.
Special recommendation:"php Programmer Toolbox" V0.1 version download
2. 3.Python basic introductory tutorial
The above is the detailed content of Solution to retry when an exception occurs. For more information, please follow other related articles on the PHP Chinese website!