Simple asynchronous execution for Django
Project description
dj-raincheck ☔️
Schedule functions to run after a request in Django without additional infrastructure.
Background tasks are great, but they are often overpowered for certain tasks and can require additional services. dj-raincheck is a simpler alternative. It will execute code after the request is complete, without the need for additional background tasks, daemons, or queues.
Installation 💻
uv add dj-raincheck
OR
pip install dj-raincheck
Usage 🧑🔧
- Add
dj_raincheckto yourINSTALLED_APPSinsettings.py.
# settings.py
INSTALLED_APPS = (
...
"dj_raincheck",
)
- Create a function that you want to run after the current request/response lifecycle.
# tasks.py
from django.core.mail import send_mail
from dj_raincheck import raincheck
@raincheck
def send_email(to: str, subject: str, body: str) -> None:
send_mail(subject, body, 'me@example.com', [to])
- Queue the function in view code to be run after the current request/response lifecycle by calling its
schedulemethod and passing in the necessary args or kwargs.
# views.py
from .tasks import send_email
# Function-based view example
def index(request):
...
send_email.schedule('customer@example.com', 'Confirm Signup', body)
return render(...)
# Class-based view example
class IndexView(View):
def get(self, request, *args, **kwargs):
...
send_email.schedule('customer@example.com', 'Confirm Signup', body)
return render(...)
Settings ⚙️
RAINCHECK_RUN_ASYNC
True by default. Set to False to execute the jobs in the current thread as opposed to starting a new thread for each function.
NOTE: This is primarily for debugging purposes. When set to False, Django will wait for all scheduled functions to complete before closing the request, which can significantly increase response times.
RAINCHECK_IMMEDIATE
False by default. Set to True to execute scheduled functions immediately when schedule() is called, rather than queuing them to run after the response is completed.
NOTE: When set to True, functions will execute synchronously during the request/response cycle.
How does this work? ✨
dj-raincheckattaches a callback to therequest_finishedsignal provided by Django- Using the
@raincheckdecorator adds aschedulefunction to the original function - Calling
schedule()queues the original function, args, and kwargs - When the current request is complete,
dj-raincheckpops all scheduled functions from the queue and starts a new thread for each one
Drawbacks 😢
dj-raincheck does not persist data to the disk or a database, so there are no guarantees if will be executed if the current request thread hangs or dies.
This is an explicit design trade-off to provide operational simplicity. dj-raincheck is useful for "fire-and-forget" tasks which, if they happen to fail, would be ok. If an application requires transactional guarantees, I recommend using django-tasks instead.
Supported Webservers 🕸️
- ✅ Django development server
- ✅
gunicorn - 🤷
uWSGI(might require enabling threads)
Please make a PR for other production webservers with your findings.
Inspiration 🙏
- Forked from django-after-response
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dj_raincheck-0.5.0.tar.gz.
File metadata
- Download URL: dj_raincheck-0.5.0.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aa48a47fa7c35806ceb4ba9c0b2e07c97e70d3ee6b6d36e531e156ba66a32804
|
|
| MD5 |
fc4aab96bae65682319f7fc2bacdb0f3
|
|
| BLAKE2b-256 |
13141f5d3415c82d8bc298dfa9f9e236ca4458528e5f8a0cf399d6eab48564a7
|
Provenance
The following attestation bundles were made for dj_raincheck-0.5.0.tar.gz:
Publisher:
publish.yml on adamghill/dj-raincheck
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dj_raincheck-0.5.0.tar.gz -
Subject digest:
aa48a47fa7c35806ceb4ba9c0b2e07c97e70d3ee6b6d36e531e156ba66a32804 - Sigstore transparency entry: 454626256
- Sigstore integration time:
-
Permalink:
adamghill/dj-raincheck@683fb8b9ad95d5125f9b60b4cc484e42aec0e8e1 -
Branch / Tag:
refs/tags/0.5.0 - Owner: https://github.com/adamghill
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@683fb8b9ad95d5125f9b60b4cc484e42aec0e8e1 -
Trigger Event:
release
-
Statement type:
File details
Details for the file dj_raincheck-0.5.0-py3-none-any.whl.
File metadata
- Download URL: dj_raincheck-0.5.0-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9bb9513ee10fb5491b42877fd01b45e331d712e6a4cd89d9d4065d12d982469c
|
|
| MD5 |
a0648e43a2f8746745efbe37cde8b65c
|
|
| BLAKE2b-256 |
27300db59d3217b7be75c82baeb5f4e61020323bdd09586d7f3d9ea965296952
|
Provenance
The following attestation bundles were made for dj_raincheck-0.5.0-py3-none-any.whl:
Publisher:
publish.yml on adamghill/dj-raincheck
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dj_raincheck-0.5.0-py3-none-any.whl -
Subject digest:
9bb9513ee10fb5491b42877fd01b45e331d712e6a4cd89d9d4065d12d982469c - Sigstore transparency entry: 454626264
- Sigstore integration time:
-
Permalink:
adamghill/dj-raincheck@683fb8b9ad95d5125f9b60b4cc484e42aec0e8e1 -
Branch / Tag:
refs/tags/0.5.0 - Owner: https://github.com/adamghill
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@683fb8b9ad95d5125f9b60b4cc484e42aec0e8e1 -
Trigger Event:
release
-
Statement type: