0Pricing
Flask Academy · บทเรียน

ติดตามผลลัพธ์และจัดการความล้มเหลว

ตรวจสอบสถานะงานและลองใหม่เมื่อเกิดข้อผิดพลาด

ติดตามผลลัพธ์และจัดการความล้มเหลว เป็นบทเรียน Flask Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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. ✅

คำถามที่พบบ่อย

บทเรียน “ติดตามผลลัพธ์และจัดการความล้มเหลว” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “ติดตามผลลัพธ์และจัดการความล้มเหลว” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Flask Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ติดตามผลลัพธ์และจัดการความล้มเหลว”

ตรวจสอบสถานะงานและลองใหม่เมื่อเกิดข้อผิดพลาด คุณปฏิบัติ Flask Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Flask Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Flask Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “ติดตามผลลัพธ์และจัดการความล้มเหลว” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Flask Academy นี้ได้ไหม

ได้ บทเรียน Flask Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. เหตุใดจึงควรย้ายงานออกจากคำขอ
  2. เชื่อม Celery เข้ากับโรงงานแอป
  3. กำหนดและเรียกใช้งาน
  4. ติดตามผลลัพธ์และจัดการความล้มเหลว
← กลับไปที่ Flask Academy