A very simple async/await background task queue for Django.
Project description
Chard
Chard is a very simple async/await background task queue for Django. One process, no threads, no other dependencies.
It uses Django's ORM to keep track of tasks.
Not very efficient or battle tested. PRs are welcome!
Parts of this were inspired by dramatiq and django_dramatiq.
Requirements
- Python 3.8+
- Django 4.1+
Installation
pip install django-chard
Quickstart
First add chard
anywhere in your INSTALLED_APPS
setting and then run
the migrations:
python manage.py migrate
Create a file called tasks.py
in one of your apps and define a task:
import chard
import httpx
from asgiref.sync import sync_to_async
from .models import MyModel
@chard.task
async def my_task(country_code):
url = f"https://somewhere.com/some-api.json?country_code={country_code}"
async with httpx.AsyncClient() as client:
resp = await client.get(url)
obj = resp.json()
for item in obj["items"]:
await sync_to_async(MyModel.objects.create)(
country_code=country_code,
item=item
)
To fire a task for the worker:
# Note that all arguments must be JSON serializable.
my_task.send("gb")
Run the worker process and it will watch for new pending tasks:
python manage.py chardworker
## Configuration
You can optionally place these in your settings.py
:
# How many tasks can run concurrently (default: 10)
CHARD_MAX_CONCURRENT_TASKS = 50
# How long a task can run until it is forcibly canceled - setting this to
# to 0 means no timeout (default: 60)
CHARD_TIMEOUT = 30
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
Hashes for django_chard-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d639f827878ae67aa16b35a06e9a86d291a7e66453f6a89eb816f7034543e088 |
|
MD5 | 8b696c98ebbacdd2e4a28db8d4e041da |
|
BLAKE2b-256 | 504e0b0f77cbaa86b3163a54f9a7f780b4c12467e0fb8c535cb9f78ed09d793f |