How to Use Redis as Broker for Celery in Django
To use
Redis as the broker for Celery in a Django project, install redis and celery packages, then set CELERY_BROKER_URL = 'redis://localhost:6379/0' in your Django settings. Initialize Celery with this broker URL to enable task queuing through Redis.Syntax
To configure Redis as the broker for Celery in Django, you need to set the broker URL in your Django settings file. This URL tells Celery where to send and receive task messages.
- CELERY_BROKER_URL: The connection string to Redis, usually
redis://localhost:6379/0. - CELERY_APP: Your Celery application instance initialized with the broker URL.
python
from celery import Celery app = Celery('your_project_name', broker='redis://localhost:6379/0') app.conf.update( result_backend='redis://localhost:6379/0', accept_content=['json'], task_serializer='json', )
Example
This example shows how to set up Celery with Redis as the broker in a Django project. It includes installing required packages, configuring settings, and creating a simple task.
bash/python
# 1. Install packages # Run in terminal: pip install celery redis # 2. In your Django settings.py CELERY_BROKER_URL = 'redis://localhost:6379/0' CELERY_RESULT_BACKEND = 'redis://localhost:6379/0' # 3. Create a celery.py file in your Django project root from celery import Celery import os os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'your_project_name.settings') app = Celery('your_project_name', broker='redis://localhost:6379/0') app.config_from_object('django.conf:settings', namespace='CELERY') app.autodiscover_tasks() # 4. Create a task in any app's tasks.py from your_project_name.celery import app @app.task def add(x, y): return x + y # 5. Run Redis server locally # 6. Start Celery worker in terminal: celery -A your_project_name worker --loglevel=info # 7. Call the task from Django shell or code: # from your_app.tasks import add # add.delay(4, 6)
Output
-------------- celery@hostname v5.x.x (singularity)
--- ***** -----
-- ******* ---- Linux-...
- *** --- * ---
- ** ---------- [config]
- ** ---------- .> app: your_project_name:0x...
- ** ---------- .> transport: redis://localhost:6379/0
- ** ---------- .> results: redis://localhost:6379/0
- *** --- * ---
-- ******* ----
--- ***** -----
[Tasks]
. your_app.tasks.add
[2024-06-xx xx:xx:xx,xxx: INFO/MainProcess] Task your_app.tasks.add[task_id] succeeded in 0.0xxs: 10
Common Pitfalls
Common mistakes when using Redis as a broker for Celery in Django include:
- Not running the Redis server before starting Celery workers.
- Using incorrect broker URL syntax (missing
redis://prefix or wrong port). - Forgetting to install the
redisPython package. - Not configuring Celery to autodiscover tasks, causing tasks not to be found.
- Using
BROKER_URLinstead ofCELERY_BROKER_URLin newer Celery versions.
python
# Wrong broker URL example app = Celery('proj', broker='localhost:6379') # Missing redis:// prefix # Correct broker URL example app = Celery('proj', broker='redis://localhost:6379/0')
Quick Reference
Summary tips for using Redis as Celery broker in Django:
- Install
celeryandredispackages. - Run Redis server locally or remotely before starting Celery.
- Set
CELERY_BROKER_URLandCELERY_RESULT_BACKENDto Redis URL insettings.py. - Initialize Celery app with the Redis broker URL.
- Use
app.autodiscover_tasks()to find tasks automatically. - Start Celery worker with
celery -A your_project_name worker --loglevel=info.
Key Takeaways
Set CELERY_BROKER_URL to 'redis://localhost:6379/0' in Django settings to use Redis as broker.
Install both celery and redis Python packages and run Redis server before starting Celery workers.
Initialize Celery app with the Redis broker URL and configure result backend similarly.
Use app.autodiscover_tasks() to ensure Celery finds your task modules automatically.
Common errors include wrong broker URL format and forgetting to run Redis server.