Define and Call a Task
Write a task and dispatch it with delay.
Define and Call a Task is a free Flask Academy lesson on CoddyKit — lesson 3 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.
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. ✅
Frequently asked questions
Is the “Define and Call a Task” lesson free?
Yes — the full text of “Define and Call a Task” 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 “Define and Call a Task”?
Write a task and dispatch it with delay. 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 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Define and Call a Task” 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.