Detailed explanation of the exception retry solution in Python, and detailed explanation of the python Solution

Source: Internet
Author: User

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.retryingProvides 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_tryFunction, thenrandom.randintIf 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 twiceretryingStay time
  • Wait_random_min and wait_random_max: Generate twice in a random wayretryingStay time
  • Wait_exponential_multiplier and wait_exponential_max: Generated twice in the form of an indexretryingThe residence time between them. The generated value is2^previous_attempt_number * wait_exponential_multiplier,previous_attempt_numberYesretryNumber of times, if the generated value exceedswait_exponential_maxSo the stop value between the next two retrying iswait_exponential_max. This design catersexponential backoffAlgorithm to reduce blocking.
  • We can specify the exceptions to be encountered before retry.retry_on_exceptionInput 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_fileIf an exception is reported during the function process, the exception will take the form parameterexceptionInputretry_if_io_errorIn the function, ifexceptionYesIOErrorThen proceed.retryIf not, stop running and throw an exception.

We can also specify the results to be obtainedretryTo use thisretry_on_resultInput 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_resultAfter successful, the return value of the function is passed through the form parameter.resultIn the formretry_if_result_noneIn the function, if the returned value isNoneThen proceed.retryOtherwise, 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.