Flask Academy · درس

تتبّع النتائج ومعالجة حالات الفشل

استطلع حالة المهمة وأعد المحاولة عند الخطأ.

الدرس 4 من 413 خطوة

تتبّع النتائج ومعالجة حالات الفشل درس مجاني في Flask Academy على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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.id

Check 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. ✅

البدء مجانًا

تعلم Python مع معلم ذكاء اصطناعي — مجانًا

اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.

الدورات
30
الدروس
120

الأسئلة الشائعة

هل درس «تتبّع النتائج ومعالجة حالات الفشل» مجاني؟

نعم — نص درس «تتبّع النتائج ومعالجة حالات الفشل» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Flask Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Flask Academy 4 دروس في المجموع.

ماذا ستتعلم في «تتبّع النتائج ومعالجة حالات الفشل»؟

استطلع حالة المهمة وأعد المحاولة عند الخطأ. تتمرن على Flask Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Flask Academy؟

لا تُشترط خبرة سابقة. Flask Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «تتبّع النتائج ومعالجة حالات الفشل»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Flask Academy هذا؟

نعم. كل درس في Flask Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. لماذا تنقل العمل خارج الطلب
  2. ربط Celery بمصنع التطبيق
  3. تعريف مهمة واستدعاؤها
  4. تتبّع النتائج ومعالجة حالات الفشل
← العودة إلى Flask Academy