Asynchronous tasks with Flask and Celery

Click for: original source

If a long-running process is part of your application’s workflow, rather than blocking the response, you should handle it in the background, outside the normal request/response flow. By Michael Herman.

By the end of this tutorial, you will be able to:

  • Integrate Celery into a Flask app and create tasks.
  • Containerize Flask, Celery, and Redis with Docker.
  • Run processes in the background with a separate worker process.
  • Save Celery logs to a file.
  • Set up Flower to monitor and administer Celery jobs and workers.
  • Test a Celery task with both unit and integration tests.

Perhaps your web application requires users to submit a thumbnail (which will probably need to be re-sized) and confirm their email when they register. If your application processed the image and sent a confirmation email directly in the request handler, then the end user would have to wait unnecessarily for them both to finish processing before the page loads or updates. Instead, you’ll want to pass these processes off to a task queue and let a separate worker process deal with it, so you can immediately send a response back to the client. Nice one!

[Read More]

Tags python web-development app-development