0Pricing
Django Academy · レッスン

@shared_taskの作成と呼び出し

delay()で関数を非同期に実行します

「@shared_taskの作成と呼び出し」はCoddyKit上の無料Django Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはDjango Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Django Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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 + y

Call 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 worker

delay 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 serializable

The 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 10s

Retry 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. 🎯

よくある質問

「@shared_taskの作成と呼び出し」レッスンは無料ですか?

はい。「@shared_taskの作成と呼び出し」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Django Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Django Academyコースには全4レッスンが含まれています。

「@shared_taskの作成と呼び出し」で何を学びますか?

delay()で関数を非同期に実行します ブラウザで直接実行するハンズオンコードでDjango Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Django Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのDjango Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。

「@shared_taskの作成と呼び出し」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このDjango Academyレッスンでコードを書いて実行できますか?

はい。すべてのDjango Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. Background Tasksが必要な理由
  2. Brokerを使ったCeleryの設定
  3. @shared_taskの作成と呼び出し
  4. Celery Beatによる定期ジョブ
← Django Academyに戻る