Writing and Calling @shared_task
Run functions asynchronously with delay().
Writing and Calling @shared_task is a free Django 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 Django Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Tasks Are Just Functions
A Celery task is an ordinary Python function with a decorator on top. The decorator teaches Celery how to queue and run it later.
Why shared_task
Use @shared_task instead of @app.task so your tasks do not depend on a specific Celery app. It is the standard choice inside Django apps.
Write Your First Task
Put tasks in a tasks.py inside an app. Decorate a plain function and Celery picks it up via autodiscover.
from celery import shared_task
@shared_task
def add(x, y):
return x + yCall It with delay
To run a task in the background, call .delay() instead of calling the function directly. It queues the job and returns at once.
add.delay(2, 3) # queued, runs on a workerdelay vs Calling Directly
Calling add(2, 3) runs it right now in your web process. Calling add.delay(2, 3) sends it to the queue. Same function, very different timing.
Pass Simple Arguments
Arguments are serialized to JSON and sent to the broker, so pass simple values like IDs and strings, not whole model objects.
send_email.delay(user.id) # good
send_email.delay(user) # avoid: not serializableThe AsyncResult Handle
delay() returns an AsyncResult. You can store its id and later check status or fetch the return value from the result backend.
result = add.delay(2, 3)
print(result.id)More Control with apply_async
Need a countdown or a specific queue? Use apply_async(), the fuller version of delay with extra options.
add.apply_async((2, 3), countdown=10) # run after 10sRetry on Failure
Bind a task and call self.retry() to re-run it after an error, perfect for flaky network calls that may succeed next time.
@shared_task(bind=True, max_retries=3)
def fetch(self):
try:
call_api()
except Exception as e:
raise self.retry(exc=e)Idempotent Tasks Win
Because tasks can retry, aim to make them idempotent: running twice should be safe and not double-charge or double-send. 🔁
Watch the Worker Log
When you call delay, watch the worker terminal. You will see the task received and its result, which is the fastest way to debug. 👀
Quick Check
How do you run a Celery task in the background?
Recap: Writing Tasks
Decorate a function with @shared_task, call it with .delay() to queue it, pass simple ids, and design tasks to retry safely. 🎯
Frequently asked questions
Is the “Writing and Calling @shared_task” lesson free?
Yes — the full text of “Writing and Calling @shared_task” is free to read here on the web, and the Django 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 Django Academy course, upgrade to CoddyKit PRO.
What will I learn in “Writing and Calling @shared_task”?
Run functions asynchronously with delay(). You practise Django 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 Django Academy?
No prior experience is required. Django 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 “Writing and Calling @shared_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 Django Academy lesson?
Yes. Every Django 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
- Why You Need Background Tasks
- Setting Up Celery with a Broker
- Writing and Calling @shared_task
- Scheduled Jobs with Celery Beat