A first look at Django's new background tasks
13 days ago
- #Task Queue
- #Background Tasks
- #Django
- Django 6.0 introduces a built-in background tasks framework in django.tasks.
- The framework handles task creation and queuing but requires external infrastructure for execution.
- The main purpose is to provide a common API for task queue implementations.
- Jake Howard is the driving force behind this enhancement.
- A reference implementation and backport for earlier Django versions is available as django-tasks on GitHub.
- The article demonstrates creating a custom backend and worker for a notification app using ntfy.sh.
- Tasks are defined using Django's standard API with the @task decorator.
- Tasks can only be run using the enqueue method, preventing accidental in-process invocation.
- The article includes a demo backend backed by a database and a worker process.
- The worker processes tasks, handles retries, and manages task statuses.
- The project demonstrates enqueuing tasks, fetching results, and handling task execution.
- The article highlights limitations like lack of built-in retries and complex orchestration.
- A custom retry decorator is provided as a workaround for automatic retries.
- The reference implementation includes actual workers, but Django 6.0 ships with only ImmediateBackend and DummyBackend.