0Pricing
Flask Academy · Lezione

Collegare Celery alla app factory

Configuri un broker e l'app Celery.

Collegare Celery alla app factory è una lezione Flask Academy gratuita su CoddyKit. Questa è la lezione 2 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Flask Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Flask Academy include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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. ✅

Domande Frequenti

La lezione «Collegare Celery alla app factory» è gratuita?

Sì — il testo completo di «Collegare Celery alla app factory» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Flask Academy, passa a CoddyKit PRO. Il corso Flask Academy include 4 lezioni in totale.

Cosa imparerò in «Collegare Celery alla app factory»?

Configuri un broker e l'app Celery. Eserciti Flask Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Flask Academy?

Non è richiesta alcuna esperienza precedente. Flask Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 2 di 4.

Quanto tempo richiede la lezione «Collegare Celery alla app factory»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Flask Academy?

Sì. Ogni lezione Flask Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Perché spostare il lavoro fuori dalla richiesta
  2. Collegare Celery alla app factory
  3. Definire e chiamare un task
  4. Tracciare i risultati e gestire gli errori
← Torna a Flask Academy