Skip to main content

Retry context manager for arbitrary code blocks

Project description

Loopretry

Loop retry is a library which allow to retry specific code block in Python.

It is different from many other retries libraries because it allows to retry a block of code, not just a lambda or a function.

Usage

from loopretry import retries
import time

for retry in retries(10):
    with retry():
        # any code you want to retry in case of exception
        assert int(time.time()) % 10 == 0, "Not a round number!"

This attempts the block up to 10 times with a 1-second delay between failures. On first success it stops. If the final attempt still fails, the exception is raised.

Typical use is waiting for eventual consistency in small integration tests.

An example for use in pytest, which waits for a metric to appear in Prometheus for up to 30 seconds:

    for retry in retries(30):
        with retry():
            assert prom_query('myapp_up{server="srv1"}') == 1.0

API

def retries(
    max_attempts: int,
    delay_sec: float = 1.0,
    expected_exception: type[BaseException] | tuple[type[BaseException], ...] = Exception,
) -> Iterable[Callable[[], ContextManager[None]]]
  • max_attempts: total number of attempts (>= 1).
  • delay_sec: delay between failed attempts.
  • expected_exception: exception class or tuple of classes to retry on. Defaults to Exception.

Returns an iterable. Each iteration yields a callable that returns a context manager. If the block raises one of expected_exception and attempts remain, the exception is swallowed, a delay is applied, and the loop continues. Otherwise, the exception is propagated.

License

MIT.

Contributing

Contributions are welcome.

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

loopretry-0.1.0.tar.gz (2.2 kB view details)

Uploaded Source

Built Distribution

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

loopretry-0.1.0-py3-none-any.whl (3.0 kB view details)

Uploaded Python 3

File details

Details for the file loopretry-0.1.0.tar.gz.

File metadata

  • Download URL: loopretry-0.1.0.tar.gz
  • Upload date:
  • Size: 2.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.5

File hashes

Hashes for loopretry-0.1.0.tar.gz
Algorithm Hash digest
SHA256 9a0b8baeb2d7931bda7c808ba0d03383c84c67f7cc5dff5184c97fd5dde7199b
MD5 6892724f169911c99c960665a6db338f
BLAKE2b-256 9ba40296704ea9eb8c62b088d02d4395840ac8a19fdd02696b48658420634a49

See more details on using hashes here.

File details

Details for the file loopretry-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: loopretry-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.5

File hashes

Hashes for loopretry-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 72d8b47282b36e771fb31deccf8a0721a2ac4d3d813182315266caa3a67ddd25
MD5 e6b67ae070493690708f5d5482ef294c
BLAKE2b-256 99f17b4fa54c6e94e2dfb179fe98b304cf52c99a46b3cd00ad4696f52265c21c

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