The retry in Python

Source: Internet
Author: User

1, through the language features to achieve:

For I in Range (0,100): When    True:        try:            # do stuff        except somespecificexception:            continue        Break

 

2, through the third-party library implementation:

Pip Install retry

Parameter description:

def retry (exceptions=exception, Tries=-1, delay=0, Max_delay=none, Backoff=1, jitter=0, Logger=logging_logger):    " "Return a retry decorator.    :p Aram Exceptions:an Exception or a tuple of exceptions to catch. Default:exception.    :p Aram tries:the Maximum number of attempts. Default:-1 (infinite).    :p Aram Delay:initial de Lay between attempts. default:0.    :p Aram max_delay:the Maximum value of delay. Default:none (No limit).    :p Aram Backoff:multiplier app Lied to delay between attempts. Default:1 (no backoff).    :p Aram Jitter:extra seconds added to delay between attempts. default:0.                   fixed if a numb Er, random if a range tuple (min, max)    :p Aram logger:logger.warning (FMT, error, delay) is called on failed Attem Pts.                   Default:retry.logging_logger. If None, logging is disabled. "" "    

Case:

From retry import retry@retry () def make_trouble ():    ' retry until succeed ' @retry (Zerodivisionerror, tries=3, delay=2) def make_trouble ():    "' Retry on Zerodivisionerror, raise error after 3 attempts, sleep 2 seconds between attem Pts. " @retry ((ValueError, TypeError), Delay=1, backoff=2) def make_trouble ():    "' Retry on ValueError or TypeError, sleep 1, 2, 4, 8, ... seconds between attempts. " @retry ((ValueError, TypeError), Delay=1, backoff=2, max_delay=4) def make_trouble ():    "' Retry on ValueError or TypeError, sleep 1, 2, 4, 4, ... seconds between attempts.  @retry (ValueError, Delay=1, jitter=1) def make_trouble ():    ' retry on ValueError, Sleep 1, 2, 3, 4, ... seconds between Attempts. " # If you enable logging, you can get warnings like ' valueerror, retrying in# 1 seconds ' if __name__ = = ' __main__ ':    impo RT logging    logging.basicconfig ()    make_trouble ()

  

Reference: Https://pypi.python.org/pypi/retry

Https://stackoverflow.com/questions/2083987/how-to-retry-after-exception-in-python

The retry in Python

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.