0Pricing
Flask Academy · Lesson

Wire Celery into the App Factory

Configure a broker and the Celery app.

Wire Celery into the App Factory is a free Flask Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flask Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Three Pieces to Wire

To run Celery you need three things: the broker URL, a result backend, and a Celery app object your tasks belong to.

Pick a Broker

Redis is the easiest broker to start with. You point Celery at it with a URL so it knows where to push and pull jobs.

broker_url = "redis://localhost:6379/0"

Add a Result Backend

If you want to read task results later, set a result backend. Redis works fine for both broker and backend in development.

result_backend = "redis://localhost:6379/1"

The Celery App Object

Celery centers on one Celery() instance. Tasks are registered on it, and workers load it to know what to run.

from celery import Celery
celery = Celery(__name__)

Why the Factory Matters

In the app factory pattern, config is loaded inside create_app. Celery should pull its broker URL from that same config, not hardcode it.

Read Config from Flask

Store CELERY settings in your Flask config so dev, test, and prod can differ. Then build Celery from app.config inside the factory.

app.config["CELERY_BROKER_URL"] = "redis://localhost:6379/0"

A make_celery Helper

A common pattern is a make_celery(app) function that reads app.config and returns a configured Celery instance.

def make_celery(app):
    c = Celery(app.import_name)
    c.conf.update(app.config)
    return c

Tasks Need App Context

Tasks often touch the database or current_app, which need an app context. Without it, those calls fail when the worker runs.

Subclass the Task Base

The classic fix wraps each task so it pushes an app context before running. That way tasks can use Flask extensions safely.

Two Processes, One Config

The web process and the worker are separate, but both build Celery from the same config so they agree on the broker and backend.

Start a Worker

You run workers from the command line, pointing at your Celery instance. Then they listen for jobs on the broker.

celery -A app.celery worker --loglevel=info

Quick Check

Tasks frequently use the database.

Recap

Set a broker and backend, build a Celery from app.config inside create_app, and push an app context so tasks can use Flask. ✅

Frequently asked questions

Is the “Wire Celery into the App Factory” lesson free?

Yes — the full text of “Wire Celery into the App Factory” is free to read here on the web, and the Flask Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flask Academy course, upgrade to CoddyKit PRO.

What will I learn in “Wire Celery into the App Factory”?

Configure a broker and the Celery app. You practise Flask Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Flask Academy?

No prior experience is required. Flask Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Wire Celery into the App Factory” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Flask Academy lesson?

Yes. Every Flask Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Why Move Work Off the Request
  2. Wire Celery into the App Factory
  3. Define and Call a Task
  4. Track Results and Handle Failures
← Back to Flask Academy