Django Tasks¶
Django’s built-in lightweight task queue (Django 6+). Uses the ORM for storage.
Use when: Simple background tasks, no external dependencies Don’t use when: Distributed processing, complex workflows (use Worker)
Requirements¶
Django 6+ django-tasks
Installation¶
pip install django-prodserver[django-tasks]
INSTALLED_APPS = [
# ...
"django-tasks",
]
python manage.py migrate
Configuration¶
PRODUCTION_PROCESSES = {
"worker": {
"BACKEND": "django_prodserver.backends.workers.django_tasks.DjangoTasksWorker",
"ARGS": {
"processes": "4",
"threads": "1",
}
}
}
ARGS¶
Argument |
Default |
Description |
|---|---|---|
|
|
Worker processes |
|
|
Threads per process |
|
|
Queue to process |
|
|
Log verbosity (0-3) |
Example¶
Web + Worker¶
PRODUCTION_PROCESSES = {
"web": {
"BACKEND": "django_prodserver.backends.servers.gunicorn.GunicornServer",
"ARGS": {"bind": "0.0.0.0:8000", "workers": "4"},
},
"worker": {
"BACKEND": "django_prodserver.backends.workers.django_tasks.DjangoTasksWorker",
"ARGS": {"processes": "2", "threads": "2"},
},
}
Defining Tasks¶
# myapp/tasks.py
from django.contrib.tasks import task
@task()
def send_email(email_address, message):
from django.core.mail import send_mail
send_mail('Subject', message, 'from@example.com', [email_address])
# Enqueue
send_email.enqueue('user@example.com', 'Hello!')
Scaling¶
Volume |
Config |
|---|---|
Low (< 1K/day) |
1 process, 2 threads |
Medium (1-10K/day) |
2 processes, 2 threads |
High (> 10K/day) |
Consider Celery |
Troubleshooting¶
Tasks not processing: Check worker is running and migrations applied
Django < 6: Upgrade Django or use Django-Q2