Constructing background tasks with Celery and Redis
It is impossible to create background processes or transactions in Flask using its flask[async]
platform. The event loop that runs tasks for the asynchronous view or endpoint will not allow the spawning of another event loop that will cater to background tasks because it cannot wait for the background processes to finish once the view or endpoint finishes its processing. However, with some third-party components, such as task queues, background processing is feasible for the Flask platform.
One of the solutions is to use Celery, which is an asynchronous task queue that can run processes outside the context of the application. So, while the event loop is running the view or endpoint coroutines, they can entrust to Celery the management of the background transactions.
Setting up the Celery task queue
There are a few considerations when writing the background processes with Celery, and the first is to install the celery
extension...