結果を追跡して失敗に対処する
タスクの状態をポーリングし、エラー時に再試行します
「結果を追跡して失敗に対処する」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはFlask Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Flask Academyコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
The AsyncResult Handle
When you enqueue a task you get an AsyncResult. Save its id so you can check on the job from any later request.
job = add.delay(2, 3)
ticket = job.idCheck the State
Each job has a state like PENDING, STARTED, SUCCESS, or FAILURE. You read it to know whether the work is done.
res = add.AsyncResult(ticket)
print(res.state)Read the Result
Once a job succeeds, its return value is stored in the backend. Access it through result on the AsyncResult.
if res.ready():
print(res.result)Poll, Do Not Block
In a web view, never call get() and wait. Instead return the task id and let the client poll a status endpoint.
A Status Endpoint
Expose a route that takes a task id and returns its state and result. The frontend hits it every few seconds until done.
@app.get("/status/<tid>")
def status(tid):
r = add.AsyncResult(tid)
return {"state": r.state}Failures Happen
External calls time out and code raises errors. When a task throws, Celery marks it FAILURE and records the exception.
Automatic Retries
Make a task retry itself on transient errors. Set max_retries and a delay so flaky calls get a few more chances.
@celery.task(bind=True, max_retries=3)
def fetch(self):
...Back Off Between Tries
Retrying instantly can hammer a struggling service. Use a growing delay, called backoff, so each retry waits longer.
Trigger a Retry
Inside the task, catch the error and call self.retry. Celery re-queues the job and counts it against max_retries.
try:
do_work()
except Exception as e:
self.retry(exc=e, countdown=5)Set Time Limits
A stuck task should not run forever. A time limit kills tasks that overrun, freeing the worker for other jobs.
Result Expiry
Stored results take space. Celery expires old results after a while, so design clients to fetch them promptly. ⏱️
Quick Check
Your task calls a flaky external API.
Recap
Track jobs by id, poll a status endpoint for state and result, and handle failures with retries, backoff, and time limits. ✅
よくある質問
「結果を追跡して失敗に対処する」レッスンは無料ですか?
はい。「結果を追跡して失敗に対処する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flask Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flask Academyコースには全4レッスンが含まれています。
「結果を追跡して失敗に対処する」で何を学びますか?
タスクの状態をポーリングし、エラー時に再試行します ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Flask Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのFlask Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「結果を追跡して失敗に対処する」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このFlask Academyレッスンでコードを書いて実行できますか?
はい。すべてのFlask Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- リクエスト外へ処理を移す理由
- Celeryをアプリファクトリに組み込む
- タスクを定義して呼び出す
- 結果を追跡して失敗に対処する