Detailed explanation of the exception retry solution in Python, and detailed explanation of the python Solution
Preface
During data capturing, you often encounter program storage problems due to network problems. Previously, you only recorded the error content and processed the error content later.
Original Process:
def crawl_page(url): pass def log_error(url): pass url = ""try: crawl_page(url)except: log_error(url)
Improved Process:
attempts = 0success = Falsewhile attempts < 3 and not success: try: crawl_page(url) success = True except: attempts += 1 if attempts == 3: break
The latest solution: retrying
Retrying is a Python retry package that can be used to automatically retry program segments that may fail to run.retrying
Provides a decorator Functionretry
, The decorated function will be re-executed when the operation fails. By default, as long as an error is reported, it will continue to retry.
import randomfrom retrying import retry @retrydef 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 runhave_a_try
Function, thenrandom.randint
If the return value is 5, the execution ends. Otherwise, the execution will continue.
Retry can also accept some parameters, which can be seen from the initialization function of the Retrying class in the source code:
stop_max_attempt_number
: Used to set the maximum number of attempts. If the maximum number of attempts is exceeded, Retry is stopped.
-
stop_max_delay
: For example, if the value is set to 10000, the time from the time when the function is executed, to the time when the function is successfully executed, or when an error is aborted when the function is successfully executed, as long as this time period exceeds 10 seconds, the function will not be executed any more.
wait_fixed
: Set it twiceretrying
Stay time
Wait_random_min and wait_random_max
: Generate twice in a random wayretrying
Stay time
Wait_exponential_multiplier and wait_exponential_max
: Generated twice in the form of an indexretrying
The residence time between them. The generated value is2^previous_attempt_number * wait_exponential_multiplier
,previous_attempt_number
Yesretry
Number of times, if the generated value exceedswait_exponential_max
So the stop value between the next two retrying iswait_exponential_max
. This design catersexponential backoff
Algorithm to reduce blocking.
- We can specify the exceptions to be encountered before retry.
retry_on_exception
Input a function 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()
In executionread_a_file
If an exception is reported during the function process, the exception will take the form parameterexception
Inputretry_if_io_error
In the function, ifexception
YesIOError
Then proceed.retry
If not, stop running and throw an exception.
We can also specify the results to be obtainedretry
To use thisretry_on_result
Input 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
In executionget_result
After successful, the return value of the function is passed through the form parameter.result
In the formretry_if_result_none
In the function, if the returned value isNone
Then proceed.retry
Otherwise, it ends and returns the function value.
Summary
The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.