Skip to main content

Retries for humans

Project description

Opnieuw

Opnieuw is a general-purpose retrying library, written in Python, in order to simplify the task of adding retry behavior to both synchronous as well as asynchronous tasks. Opnieuw is easy and straightforward to use.

Why Opnieuw? Most retry packages lack either adequate documentation on how to properly use the package and or are difficult to customize. Valuable time is lost in trying to figure out how to properly utilize a retry package. Opnieuw makes it incredibly easy to add retry functionality to any task that requires retry.

Suppose we want to parse https://tech.channable.com/atom.xml and we want to add a retry to handle a specific network Exception. We can add Opnieuw to our network handler as follows:

import requests
from requests.exceptions import ConnectionError

from opnieuw import retry

@retry(
    retry_on_exceptions=ConnectionError,
    max_calls_total=4,
    retry_window_after_first_call_in_seconds=60,
)
def get_page() -> str:
    response = requests.get('https://tech.channable.com/atom.xml')
    return response.text

In the above decorator, retry_on_exceptions refers to exceptions we want to retry on, max_calls_total maximal number of retry attempts we want to make, and retry_window_after_first_call_in_seconds refers to retry window within which the max_calls_total will be made. In this case we want to make a maximum of 4 retries spread uniformly on a 60 second interval.

Features

  • Generic Decorator API
  • Specify retry exception (i.e. kind of exception that we want retry)
  • Specify retry window after first call in seconds (i.e. exponential backoff sleeping between attempts)
  • Pre-shipped list of popular exceptions, which can easily be expanded

Installation

To install Opnieuw, simply:

$ pip install opnieuw

Examples

The short example above provides a concise demonstration of how Opnieuw could be used. Let's dig deeper into Opnieuw and add another exception to retry_on_exceptions to do a retry on:

from urllib.error import URLError
import requests
from opnieuw import RetryException, retry

@retry(
    retry_on_exceptions=(ConnectionError, RetryException, URLError),
    max_calls_total=4,
    retry_window_after_first_call_in_seconds=60,
)
def get_page() -> str:
    response = requests.get('https://tech.channable.com/atom.xml')

    if response.status_code != 200:
        raise RetryException

    return response.text

We can pass the name of exception we want do retry for or a tuple of exceptions to the retry_on_exceptions. As mentioned earlier Opnieuw was developed to make it more convenient to add retry behavior to any task.

Let's make it a little bit more generic and define a list of retry exceptions that should trigger a retry of the function:

STANDARD_HTTP_EXCEPTIONS = (
    ConnectionError,
    ProtocolError,
    RetryException,
    ...
)

@retry(
    retry_on_exceptions=STANDARD_HTTP_EXCEPTIONS
    max_calls_total=4,
    retry_window_after_first_call_in_seconds=60,
)
def get_page() -> str:
    response = requests.post('https://tech.channable.com/atom.xml')
    return response.text

Now our retry is more generic, as exceptions raised which are in STANDARD_HTTP_EXCEPTIONS will be retried.

If you want retry behavior for async tasks, then there is also an async retry which basically work the same way, but for async tasks.

Here is the example above but in async mode:

from opnieuw import retry_async

STANDARD_HTTP_EXCEPTIONS = (
    ConnectionError,
    EOFError,
    RetryException,
    ...
)

@retry_async(
    retry_on_exceptions=STANDARD_HTTP_EXCEPTIONS,
    max_calls_total=4,
    retry_window_after_first_call_in_seconds=60,
)
async def get_page() -> str:
    response = requests.get('https://tech.channable.com/atom.xml')
    return response.text

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

opnieuw-0.0.3.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

opnieuw-0.0.3-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file opnieuw-0.0.3.tar.gz.

File metadata

  • Download URL: opnieuw-0.0.3.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.6.9

File hashes

Hashes for opnieuw-0.0.3.tar.gz
Algorithm Hash digest
SHA256 6db225f48ebcd68706f5281942f8810910258544ff245f18334d1ea9db25bcfa
MD5 bf745df86b764bd267dbd2f4a1ba75a1
BLAKE2b-256 dc5c9a1e0cf046c05aa41f87f807e0f97666f19d6e74379cc152ab0ed1696c0c

See more details on using hashes here.

File details

Details for the file opnieuw-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: opnieuw-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.6.9

File hashes

Hashes for opnieuw-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d30d016650bef47a2bd0be4f15bc814f514c75c96aa496fa641744a67049e38e
MD5 b19801bc7d587d14d92ded9c895dc401
BLAKE2b-256 7d4c967fb92becb0b66d32a29bf9a0b8abceeb793dc9f28e6751c465c6e12026

See more details on using hashes here.

Supported by

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