Definire e chiamare un task
Scriva un task e lo distribuisca con delay.
Definire e chiamare un task è una lezione Flask Academy gratuita su CoddyKit. Questa è la lezione 3 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.
What a Task Is
A Celery task is just a Python function marked so workers can run it later. You decorate it once and it becomes enqueueable.
The task Decorator
Mark a function with the Celery task decorator. Now it has extra methods like delay that schedule it onto the queue.
@celery.task
def add(x, y):
return x + yCall It Normally vs Queued
Calling add(2, 3) runs it right now in your process. To run it on a worker instead, you call its delay method.
Enqueue with delay
The delay shortcut puts the job on the broker and returns immediately. The work happens later on a worker.
add.delay(2, 3)apply_async for Options
Need more control like a countdown or queue name? Use apply_async, which takes args plus scheduling options.
add.apply_async(args=[2, 3], countdown=10)You Get an AsyncResult
Both delay and apply_async return an AsyncResult. It carries the task id you use to look up the result later.
job = add.delay(2, 3)
print(job.id)Arguments Must Serialize
Task arguments travel through the broker, so they must be serializable: use plain data like ints, strings, lists, and dicts.
Pass IDs, Not Objects
Do not send a database row to a task. Send its id instead, then re-fetch the object inside the task with a fresh session.
Enqueue Inside a View
A view validates input, enqueues the job, and returns a fast response. The user is not kept waiting for the slow part.
@app.post("/report")
def report():
add.delay(2, 3)
return {"status": "queued"}Name Tasks Clearly
Give tasks descriptive names like send_welcome_email. Clear names make logs and the broker dashboard far easier to read. 🔍
Keep Tasks Small
A good task does one clear job. Small, focused tasks are easier to retry, monitor, and reason about when something breaks.
Quick Check
You want a function to run on a worker.
Recap
Decorate a function as a task, call delay or apply_async to queue it, pass serializable args, and enqueue from thin views. ✅
Domande Frequenti
La lezione «Definire e chiamare un task» è gratuita?
Sì — il testo completo di «Definire e chiamare un task» è 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 «Definire e chiamare un task»?
Scriva un task e lo distribuisca con delay. 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 3 di 4.
Quanto tempo richiede la lezione «Definire e chiamare un task»?
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
- Perché spostare il lavoro fuori dalla richiesta
- Collegare Celery alla app factory
- Definire e chiamare un task
- Tracciare i risultati e gestire gli errori