0Pricing
Flask Academy · Lesson

Track Results and Handle Failures

Poll task state and retry on error.

Track Results and Handle Failures is a free Flask Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flask Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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

Frequently asked questions

Is the “Track Results and Handle Failures” lesson free?

Yes — the full text of “Track Results and Handle Failures” is free to read here on the web, and the Flask Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flask Academy course, upgrade to CoddyKit PRO.

What will I learn in “Track Results and Handle Failures”?

Poll task state and retry on error. You practise Flask Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Flask Academy?

No prior experience is required. Flask Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Track Results and Handle Failures” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Flask Academy lesson?

Yes. Every Flask Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Why Move Work Off the Request
  2. Wire Celery into the App Factory
  3. Define and Call a Task
  4. Track Results and Handle Failures
← Back to Flask Academy