flask/examples/celery
Tang Vu d2e34be0f6 refactor: celery task result endpoint crashes on task failure
When a Celery task fails, `result.ready()` evaluates to `True` but `result.successful()` is `False`. 
Calling `result.get()` without `propagate=False` on a failed task will re-raise the task's exception, causing the Flask endpoint to crash with a 500 Internal Server Error instead of returning the task's failed status. 
Additionally, if `result.result` is an Exception object, returning it directly in the dictionary will cause a `TypeError` during JSON serialization.


Affected files: views.py

Signed-off-by: Tang Vu <vuminhtang2212@gmail.com>
2026-03-28 04:19:28 +07:00
..
src/task_app refactor: celery task result endpoint crashes on task failure 2026-03-28 04:19:28 +07:00
make_celery.py add celery example 2023-02-10 09:16:53 -08:00
pyproject.toml update example project metadata 2024-11-01 18:00:39 -07:00
README.md show 'run --debug' in docs 2023-02-15 14:33:32 -08:00
requirements.txt update dependencies 2023-05-02 07:17:52 -07:00

Background Tasks with Celery

This example shows how to configure Celery with Flask, how to set up an API for submitting tasks and polling results, and how to use that API with JavaScript. See Flask's documentation about Celery.

From this directory, create a virtualenv and install the application into it. Then run a Celery worker.

$ python3 -m venv .venv
$ . ./.venv/bin/activate
$ pip install -r requirements.txt && pip install -e .
$ celery -A make_celery worker --loglevel INFO

In a separate terminal, activate the virtualenv and run the Flask development server.

$ . ./.venv/bin/activate
$ flask -A task_app run --debug

Go to http://localhost:5000/ and use the forms to submit tasks. You can see the polling requests in the browser dev tools and the Flask logs. You can see the tasks submitting and completing in the Celery logs.