Run processes in the background with a separate worker process.Containerize FastAPI, Celery, and Redis with Docker.Integrate Celery into a FastAPI app and create tasks.Contentsīy the end of this tutorial, you will be able to:
Finally, we'll look at how to test the Celery tasks with unit and integration tests. We'll also use Docker and Docker Compose to tie everything together. To achieve this, we'll walk you through the process of setting up and configuring Celery and Redis for handling long-running processes in a FastAPI app. Your application is also free to respond to requests from other users and clients. The end user can then do other things on the client-side while the processing takes place.
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. 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. 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 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.