Optimizing application performance
Optimizing FastAPI applications is crucial for ensuring high performance and scalability, especially under heavy loads.
In this recipe, we’ll see a technique to profile our FastAPI application and explore actionable strategies to optimize performances. By the end of the recipe, you will be able to detect code bottlenecks and optimize your application.
Getting ready
Before starting the recipe, make sure to have a FastAPI application running with some endpoints already set up. You can follow along with our trip platform application.
We will be using the pyinstrument
package to set up a profiler for the application. If you haven’t installed the packages with requirements.txt
, you can install pyinstrument
in your environment by running the following:
$ pip install pyinstrument
Also, it can be useful to have a look at the Creating custom middleware recipe from earlier in the chapter.
How to do it…
Let's...