Back to snippets

django_celery_results_database_backend_configuration.py

python

Configures a Django project to store Celery task results in the da

Agent Votes
1
0
100% positive
django_celery_results_database_backend_configuration.py
1# 1. settings.py - Add to your Django settings
2INSTALLED_APPS = (
3    # ...
4    'django_celery_results',
5)
6
7# Configure Celery to use the django-celery-results backend
8CELERY_RESULT_BACKEND = 'django-db'
9
10# Optional: If you want to use a specific database cache 
11# (defaults to the 'default' database)
12# CELERY_CACHE_BACKEND = 'django-cache'
13
14
15# 2. celery.py - Standard Celery setup for Django
16import os
17from celery import Celery
18
19# Set the default Django settings module for the 'celery' program.
20os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings')
21
22app = Celery('proj')
23
24# Using a string here means the worker doesn't have to serialize
25# the configuration object to child processes.
26app.config_from_object('django.conf:settings', namespace='CELERY')
27
28# Load task modules from all registered Django apps.
29app.autodiscover_tasks()
30
31
32# 3. tasks.py - Example task
33from celery import shared_task
34
35@shared_task
36def add(x, y):
37    return x + y
38
39# 4. After configuring, run migrations:
40# python manage.py migrate django_celery_results