タスクを定義して呼び出す
タスクを書き、delayでディスパッチします
「タスクを定義して呼び出す」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはFlask Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Flask Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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. ✅
よくある質問
「タスクを定義して呼び出す」レッスンは無料ですか?
はい。「タスクを定義して呼び出す」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flask Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flask Academyコースには全4レッスンが含まれています。
「タスクを定義して呼び出す」で何を学びますか?
タスクを書き、delayでディスパッチします ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Flask Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのFlask Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。
「タスクを定義して呼び出す」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このFlask Academyレッスンでコードを書いて実行できますか?
はい。すべてのFlask Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- リクエスト外へ処理を移す理由
- Celeryをアプリファクトリに組み込む
- タスクを定義して呼び出す
- 結果を追跡して失敗に対処する