Configurando Celery com um intermediário
Conecte Celery ao Redis ou RabbitMQ
Configurando Celery com um intermediário é uma aula grátis de Django Academy no CoddyKit. Esta é a aula 2 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Django Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Django Academy inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
What a Broker Is
Celery needs a broker: a middleman that holds queued tasks until a worker grabs them. Think of it as the inbox between your app and the workers.
Redis or RabbitMQ
The two common brokers are Redis (simple, fast, great default) and RabbitMQ (heavier, very robust). For most Django apps, Redis is plenty. 📨
Install the Packages
Start by installing Celery and the Redis client so Celery can talk to your broker over the network.
pip install celery redisCreate the Celery App
Add a celery.py next to settings.py. It creates the Celery instance and tells it where to find your Django config.
import os
from celery import Celery
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
app = Celery("myproject")Read Config from Settings
Point Celery at your Django settings so all its options live in one place, namespaced with a CELERY prefix.
app.config_from_object("django.conf:settings", namespace="CELERY")Auto-Discover Tasks
One line tells Celery to scan every installed app for a tasks module, so you never have to register tasks by hand.
app.autodiscover_tasks()Wire It into __init__
In your project __init__.py, import the Celery app so it loads whenever Django starts. This makes the shared task decorator work.
from .celery import app as celery_app
__all__ = ("celery_app",)Set the Broker URL
In settings.py, point CELERY_BROKER_URL at your running Redis instance so producers and workers share the same queue.
CELERY_BROKER_URL = "redis://localhost:6379/0"Add a Result Backend
If you want to read task results later, set a result backend. Redis can serve double duty as both broker and result store.
CELERY_RESULT_BACKEND = "redis://localhost:6379/0"Start a Worker
Run the worker from a terminal. It connects to the broker and waits for jobs. Keep your Django server running in a separate terminal. 🛠️
celery -A myproject worker -l infoTwo Processes, Always
Remember: the web server and the worker are separate processes. Both must run for background tasks to actually execute.
Quick Check
What job does the broker do in a Celery setup?
Recap: Celery Setup
Install Celery and a broker like Redis, create celery.py, set the broker URL, and run a worker in its own terminal. You are ready to queue. ✅
Aprenda Python com um tutor de IA — grátis
Escreva e execute código real no seu navegador, obtenha ajuda instantânea de um tutor de IA 24/7 e continue de onde parou na web ou no app.
- Cursos
- 30
- Aulas
- 120
Perguntas Frequentes
A aula “Configurando Celery com um intermediário” é grátis?
Sim — o texto completo de “Configurando Celery com um intermediário” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Django Academy, atualize para CoddyKit PRO. O curso de Django Academy inclui 4 aulas no total.
O que vou aprender em “Configurando Celery com um intermediário”?
Conecte Celery ao Redis ou RabbitMQ Você pratica Django Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Django Academy?
Nenhuma experiência prévia é necessária. Django Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 2 de 4.
Quanto tempo leva a aula “Configurando Celery com um intermediário”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Django Academy?
Sim. Cada aula de Django Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Por que você precisa de tarefas em segundo plano
- Configurando Celery com um intermediário
- Escrevendo e chamando @shared_task
- Tarefas agendadas com Celery Beat