Quick Start¶
1. Start a broker¶
Valkey/Redis:
Or RabbitMQ:
2. Create your app¶
# celeryapp.py
from celery import Celery
app = Celery("myapp")
app.config_from_object({
"broker_url": "redis://localhost:6379/0",
"result_backend": "redis://localhost:6379/1",
"include": ["tasks"],
})
3. Define tasks¶
# tasks.py
import asyncio
from celeryapp import app
@app.task
def add(x, y):
return x + y
@app.task
async def slow_add(x, y):
await asyncio.sleep(1)
return x + y
Sync tasks (add) run in a thread pool. Async tasks (slow_add) run directly on the asyncio event loop.
4. Start the worker¶
The -E flag enables events for monitoring with Flower.
5. Send tasks¶
from tasks import add, slow_add
# Fire and forget
add.delay(2, 3)
# With options
add.apply_async(args=(2, 3), countdown=10)
# Async task
slow_add.delay(10, 20)
6. Monitor with Flower (optional)¶
Open http://localhost:5555 to see workers, tasks, and live graphs.
Using RabbitMQ¶
Same setup, just change the broker URL:
app.config_from_object({
"broker_url": "amqp://guest:guest@localhost:5672//",
"result_backend": "redis://localhost:6379/1",
"include": ["tasks"],
})
The AMQP transport uses aio-pika under the hood. Install it with: