Learn how to leverage Celery library, Redis database and SocketIO library to build synchronous and asynchronous tasks with Flask in Python.

Legacy web applications are synchronous in nature. The user interacts with a web interface presented in the browser, the browser makes requests back to the server based on that user interaction, and the server responds to those requests with a new presentation for the user.

Nowadays, things have changed modern-day websites need to handle requests from hundreds of thousands of visitors. When these requests involve interaction with a database or a web service, response time increases, and when thousands of visitors are accessing the same resources, website performance can drastically decrease.  Here Asynchronous web come to the rescue .

Here are some of the benefits we may grasp when opting for asynchronicity:

  • Ability to handle more requests
  • Parallel execution of I/O bound methods.
  • Improved responsiveness.

Scope

In this tutorial, we will explain how to overcome one of the common pitfalls encountered when building a web application which is handling long running tasks that limit the ability of the web server to respond to new requests.

A straightforward solution would be to run these long running tasks asynchronously in background, within a separate thread or process, freeing up the web server.

We will leverage several components like Redis, Flask, Celery, and SocketIO to offload the execution of a long running task and once completed send a push notification to the client indicating its status.

It is worth noting that this tutorial will not cover asyncio Python’s built-in library, which allows us to run code concurrently using coroutines.

#celery #python #redis #socketio #database

Asynchronous Tasks with Celery in Python
3.85 GEEK