A simple async/await task queue for Django. One process, no threads, no other dependencies.
Chard is a 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.
π Documentation
π Check the example Django project
pip install django-chard
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
To see a full example of Chard in action:
π Check the example Django project
Please see CONTRIBUTING the contributing guidelines.
Please see LICENSE for licensing details.
0.2 (2022-09-16)
- Type hinting
- Return a task ID when queueing a task
- Added docs
- Tidying and bug fixes