Skip to main content

Reloadable

Build Status codecov Python Versions pypi

Reruns a function upon failure

Usage

Reloadable

The function my_func will run indefinitely until it stops raising exceptions, which will never happen in this case.

from reloadable import reloadable

@reloadable()
def my_func():
    raise Exception('Oops')

This module is useful when we want to run something forever, like a code that connects to a queue en fetches messages. Eventually it may disconnect and raise an error trying to fetch a message, so reloadable can retry connecting.

@reloadable()
def get_message():
    conn = Queue(host='...', password='...')

    while True:
        message = conn.fetch_message()
        # probably process message afterwards...

You can config a callback function that receives an exception, which will be called if it occurs.

def shit_happens(exception):
    logger.exception(exception)

@reloadable(exception_callback=shit_happens)
def dont_stop():
    raise Exception('Deal with it')

You can also wait some time before the next respawn

@reloadable(sleep_time=7)  # wait 7 seconds before running `get_message` after a failure
def get_message():
    # some code...

You can always stop reloadable with a KeyboardInterrupt exception (usually triggered by ^C, but not necessarily).

Another option is to configure the stop condition exception.

@reloadable(stop_condition_exception=ValueError)
def i_will_stop():
    raise ValueError('something went wrong')

Or you can define it globally, which will be used if local stop condition wasn’t defined

from reloadable import reloadable, configure

configure(stop_condition_exception=KeyError)

@reloadable()
def i_will_stop():
    raise KeyError('...')

You may also want to limit the number of times that the decorator should try rerun the function. If the function is called max_reloads times without a success, it raises the last error.

from reloadable import reloadable

@reloadable(max_reloads=2)
def a_func():
    raise KeyError('...')

Alternatively you can disable the reloadable decorator via configuration, which is useful during unittests.

from reloadable import configure, reloadable

configure(enabled=False)

@reloadable()  # When disabled, it does nothing
def i_am_free():
    return '\o/'

Retry on Error

The @retry_on_error decorator is useful when you want to retry something on error, but return the result once the decorated function finishes it’s execution with success.

import requests
from reloadable.decorators import retry_on_error


@retry_on_error(max_reloads=3)
def my_request():
    response = requests.get("https://www.sieve.com.br")

    # raises an error for 4xx and 5xx status codes
    response.raise_for_status()

    return response.content

Tests

python -m unittest -v tests

Installation

pip install reloadable

Metadata

Release files for reloadable 0.1.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for reloadable 0.1.5
File Size Uploaded Provenance Actions
reloadable-0.1.5.macosx-10.13-intel.tar.gz 4.1 kB Without provenance Details for reloadable-0.1.5.macosx-10.13-intel.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for reloadable 0.1.5
File Interpreter ABI Platform Actions
reloadable-0.1.5-py2.py3-none-any.whl Python 2, Python 3 none any Details for reloadable-0.1.5-py2.py3-none-any.whl

Total release size: 10.3 kB

Release files / reloadable-0.1.5.macosx-10.13-intel.tar.gz

Download URL reloadable-0.1.5.macosx-10.13-intel.tar.gz
Size 4.1 kB
Tags Source
SHA-256 checksum
How to use checksums
c0f5ea13a0f5652bf3e9bb192ba5d911750b0a4923ed77b3d018f64a74b5d2b2
BLAKE2b-256 checksum
How to use checksums
4469edeb78ca74b32820840242ce0a18ae2fc93d55a56349712bffb1a0a0c787
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / reloadable-0.1.5-py2.py3-none-any.whl

Download URL reloadable-0.1.5-py2.py3-none-any.whl
Size 6.2 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
d8c99330c377ea53ae06ae2e7e2a4dea82e753dbafaea618c004bba44534eb8f
BLAKE2b-256 checksum
How to use checksums
de80e8ba1c2740ebcdee84839c625064dac02f07e917aef545b923a358ee78c1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for reloadable 0.1.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page