DEV Community

Arhan Khan
Arhan Khan

Posted on

Celery for Beginners: Run Background Jobs in Python Without Blocking Requests

Celery is the standard way to run background jobs in Python. Some work should not happen inside a web request: sending an email, resizing an image, generating a report. Celery lets your web app hand that work to a queue and answer the user immediately, while worker processes pick the jobs up and run them in the background.

How it works: your app creates a task — a Python function decorated with @app.task — and calls it with .delay() instead of calling it directly. The call goes to a message broker (usually Redis or RabbitMQ) as a message. A Celery worker, a separate process, reads the message, runs the function, and stores the result. Your web request never waits for the slow work to finish.

Celery is a good fit for beginners because the core idea is one decorator and one method call. You can run the broker, the worker, and your app all on your laptop while learning, and the same code scales to separate servers later. The result backend lets you check on a job's status, so "is my report ready yet?" is a simple query.

A quick example — sending a welcome email in the background:

from celery import Celery

app = Celery("tasks", broker="redis://localhost:6379/0")

@app.task
def send_welcome_email(user_id):
    user = get_user(user_id)
    send_email(user.email, "Welcome aboard!")

# In your web request — returns instantly, email sends in the background
send_welcome_email.delay(new_user.id)
Enter fullscreen mode Exit fullscreen mode

The user sees "account created" in milliseconds; the email goes out a moment later, from a worker.

Who is it for? Python developers whose web apps do slow work; teams adding email, reports, or image processing to a Django or FastAPI project; students learning how background processing actually works.

Limits to know: Celery adds moving parts — a broker, workers, and a result store — so debugging gets harder when a job fails silently. Tasks must be idempotent (safe to run twice), because retries happen. And for very simple needs, a lighter queue or even a cron job may be enough.

To start today: install Celery with Redis, write one task that sleeps for five seconds, call it with .delay(), and watch the worker pick it up while your script continues.

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.