Skip to content

Retry a function in the background asynchronously without blocking the flow of execution

License

Notifications You must be signed in to change notification settings

krishnasism/retry-later

Repository files navigation

latest tests Coverage Status

retry_later

@retry_later allows you to retry the execution of a function asynchronously in the background until it's done.

Why?

For example, you can retry sending an email until it's sent.

Installation

It's available on PyPI

pip install retry-later

Local Installation

# Clone repository
git clone https://github.com/krishnasism/retry-later.git
cd retry-later

# Create a virtual environment. You can use anything like venv or pipenv
python -m venv retry-later-venv
retry-later-venv/Scripts/activate

# Install dev dependencies
pip install -e ".[dev]"

# Install pre-commit hooks
pre-commit install

Usage

Simply add @retry_later() to your function. This also works with async functions.

Look inside the examples folder for more examples!

Here's a simple demonstration:

from my_very_python_real_email_client import send_mail_to_friend, send_text_to_friend
import asyncio

# Create new event loop
loop = asyncio.new_event_loop()

# Import the decorator
from retry_later import retry_later

@retry_later(retry_interval=10, max_retries=5, exception=ConnectionError)
def send_email():
    send_mail_to_friend(email="[email protected]", body="hi!")

@retry_later(retry_interval=10, max_retries=5, exception=ConnectionError)
async def send_a_text():
    await send_text_to_friend(number="+123456789", body="hi! texting you")

send_email()
asyncio.run(send_a_text())

# Other stuff
print("Hi - I am here!! I complete before my_function!")

# event loop must be running for retry_later to work
loop.run_forever()