Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Retry function for sending data

import time
import math
from functools import wraps

def retry(ExceptionToCheck, tries=4, delay=3, backoff=2, logger=None):
    """Retry calling the decorated function using an exponential backoff.
    http://www.saltycrane.com/blog/2009/11/trying-out-retry-decorator-python/
    original from: http://wiki.python.org/moin/PythonDecoratorLibrary#Retry
    :param ExceptionToCheck: the exception to check. may be a tuple of
        exceptions to check
    :type ExceptionToCheck: Exception or tuple
    :param tries: number of times to try (not retry) before giving up
    :type tries: int
    :param delay: initial delay between retries in seconds
    :type delay: int
    :param backoff: backoff multiplier e.g. value of 2 will double the delay
        each retry
    :type backoff: int
    :param logger: logger to use. If None, print
    :type logger: logging.Logger instance
    """
    def deco_retry(f):

        @wraps(f)
        def f_retry(*args, **kwargs):
            mtries, mdelay = tries, delay
            while mtries > 1:
                try:
                    return f(*args, **kwargs)
                except ExceptionToCheck as e:
                    msg = "%s, Retrying in %d seconds..." % (str(e), mdelay)
                    if logger:
                        logger.warning(msg)
                    else:
                        print(msg)
                    time.sleep(mdelay)
                    mtries -= 1
                    mdelay *= backoff
            return f(*args, **kwargs)

        return f_retry  # true decorator

    return deco_retry
Comment

PREVIOUS NEXT
Code Example
Python :: sqlalchemy validation at db level 
Python :: EDA dataframe missing and zero values 
Python :: Invenco Order Dict 
Python :: replace dataframe column element if element is within a specific list 
Python :: Best websites to learn Python 
Python :: how to scrape data from github api python 
Python :: ring For in Loop 
Python :: ring open another file 
Python :: protilipi get text python 
Python :: can you make a class in a class python 
Python :: send whats app message using python 
Python :: get length of list python 
Python :: twitter api ("Connection broken: Invalid Chunk Length(got length b', 0 bytes read)" 
Python :: how to create dataframe from rdd 
Python :: how to download feature engine in spyder console 
Python :: consider a string note: "welcome" statment will rais error 
Python :: pygame mixer channel loop 
Python :: seleniu get element value and store it in a variable - selenium remember user 
Python :: gspread how to put shhet number in a variable 
Python :: how to load images from folder in python 
Python :: python how to compress pytorch model 
Python :: view does not return httpresponse 
Python :: loop only to the 6th element python 
Python :: replace substrings to float 
Python :: ‘A’, ‘Q’, ‘BM’, ‘BA’, ‘BQ’ meaning in resample 
Python :: Pandas automatic allignment of columns 
Python :: 1045 uri solution 
Python :: Print Multiple Variables 
Python :: Iterate over several iterables in parallel 
Python :: reverse bolean python 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =