@shared_task schreiben und aufrufen
Funktionen mit delay() asynchron ausführen
@shared_task schreiben und aufrufen ist eine kostenlose Django Academy-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Django Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Django Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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. 🎯
Häufig gestellte Fragen
Ist die Lektion „@shared_task schreiben und aufrufen“ kostenlos?
Ja — der vollständige Text von „@shared_task schreiben und aufrufen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Django Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Django Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „@shared_task schreiben und aufrufen“?
Funktionen mit delay() asynchron ausführen Du übst Django Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Django Academy zu starten?
Keine Vorkenntnisse erforderlich. Django Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.
Wie lange dauert die Lektion „@shared_task schreiben und aufrufen“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Django Academy-Lektion Code schreiben und ausführen?
Ja. Jede Django Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Warum Sie Background Tasks benötigen
- Celery mit einem Broker einrichten
- @shared_task schreiben und aufrufen
- Geplante Jobs mit Celery Beat