Skip to main content

Smart retry library

Project description

Library to make code more robust Build Status

Retry decorator parameters

retry(attempts_number, delay=0, step=0, max_delay=-1, retry_on=Exception, logger=None)

  • attempts_number: number of function calls (first call + retries). If attempts_number < 0 then retry infinitely
  • delay: delay before first retry
  • step: increment value of timeout on each retry
  • max_delay: maximum delay value (upper bound for delay)
  • retry_on: exception that should be handled or function that checks if retry should be executed (default: Exception)
  • logger: logger to write warnings

returns the result of decorated function

Retry on specific exception

from retrylib import retry

@retry(attempts_number=3, retry_on=(MyException,))
def function():
  raise MyException()

Use custom function

from retrylib import retry

def is_my_exception(error):
  return isinstance(error, MyException)

@retry(attempts_number=3, retry_on=is_my_exception)
def function():
  raise MyException()

Retry on network errors

You can use following code to add retries for your custom network function:

import requests
from retrylib.network import retry

@retry()
def function():
 response = requests.get('http://localhost:5002')
 response.raise_for_status()
 return response

function()

Logging

Global logger: you can pass specific logger to decorator

import logging
import logging.config

from retrylib.network import retry

LOGGING = {
  'version': 1,
  'formatters': {
      'precise': {
          'datefmt': '%Y-%m-%d,%H:%M:%S',
          'format': '%(levelname)-7s %(asctime)15s '
                    '%(name)s:%(lineno)d %(message)s'
      }
  },
  'handlers': {
      'console': {
          'class': 'logging.StreamHandler',
          'formatter': 'precise',
          'stream': 'ext://sys.stderr'
      },
  },
  'root': {
      'level': 'INFO',
      'handlers': ['console']
  }
}

logging.config.dictConfig(LOGGING)

LOGGER = logging.getLogger(__name__)

@retry(logger=LOGGER)
def function():
 response = requests.get('http://localhost:5002')
 response.raise_for_status()
 return response

Object-specific logger: to use object-specific logger define method 'get_logger'

from retrylib import retry

class MyClass(object):
 def __init__(self):
     self._logger = logging.getLogger(__name__)

 def get_logger(self):
     return self._logger

 @retry()
 def my_method(self):
     ...
     raise Exception

obj = MyClass()
obj.my_method()
# obj._logger will be used

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

retrylib-1.2.5.tar.gz (12.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

retrylib-1.2.5-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

Details for the file retrylib-1.2.5.tar.gz.

File metadata

  • Download URL: retrylib-1.2.5.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.11.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.3

File hashes

Hashes for retrylib-1.2.5.tar.gz
Algorithm Hash digest
SHA256 1dd86ad0ad329fe28df76bb84661c93add6c57945c34efa035b6a5dfbd8d64aa
MD5 f5b648bd17a524489104c3f3c613c003
BLAKE2b-256 96b544ba22b77f19d32a34e2c7b5b5ee22d5f3ade2f474be8924b0b61055fa6f

See more details on using hashes here.

File details

Details for the file retrylib-1.2.5-py3-none-any.whl.

File metadata

  • Download URL: retrylib-1.2.5-py3-none-any.whl
  • Upload date:
  • Size: 10.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.11.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.3

File hashes

Hashes for retrylib-1.2.5-py3-none-any.whl
Algorithm Hash digest
SHA256 75b836d813c433e43d14356d8cfe75b61109fb9a3e4c18b749a8cffebbceabb3
MD5 c1589b5637b64e1fbc44953ed4c03a97
BLAKE2b-256 b11a9d9c45e079039b7ae906b0e50a5a8b226982525d1c7df26882f29e074b83

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page