Why choose FastAPI

Tram Ho

Introduction

Recently, due to the growing need for microservice development, I mainly code Python – Backend, so I was allowed to choose a framework to develop new projects for the company, after considering between 3 popular frameworks. At using Python as Django, Flask and FastAPI, I decided to choose FastAPI to develop the project, here is the reason for this.

Main Reason

FastAPI is a fairly new micro framework, just released in 2018. The Github framework of this framework is currently reaching 25.4k stars as of January 2021, but due to the application of many new technologies, FastAPI has a few points. Strong, but I feel quite suitable for use in project development:

  • High performance Due to being based on two fairly strong libs at the moment of python, Pydantic and Starlette, FastAPI has the highest performance out of all current Python frameworks, you can refer to the performance comparison between frameworks. at https://www.techempower.com/
  • Development Speed ​​With built-in Swagger interface – OpenAPI comes with a fairly simple code, so programmers can release the function very quickly and still have full documentation, this is the most important advantage of FastAPI compared to with other frameworks. Below is a code that prints the healthcheck text

  • Asynchronous Currently asynchronous has been supported since Django 3.x but since release, FastAPI has supported Async developer by default, so FastAPI can only be used with python3.6 and above.

Compare Django, Flask and FastAPI

Below is a comparison table between the 3 Frameworks Django, Flask and FastAPI based on my own experience in experiencing all 3 frameworks.

DjangoFlaskFastAPI
CommunityDjango’s community is currently quite large and long-standing, so the number of lib support for Django is arguably the most at the moment.The current Flask community is also large but not as large as Django, so the number of libs is also limited than DjangoFastAPI’s community is relatively new, so there are few libs that support it
Python versionAll version: 2x, 3xAll version: 2x, 3xOnly python3.x
PerformanceDue to support for many features, Django’s perfomance is not highPerformance is quite highThe highest performing framework in current python frameworks
ORMDjango ORMFlask-sqlachemySqlachemy
AsyncSupport from Django 3.xNot yet supportedCompletely compatible
Builtin Admin UIHaveAre notAre not
Project FrameYes, just run python manage startapp yourappAre notAre not
Python shellHaveHaveNo – this is a limitation of a microframework compared to real frameworks
OpenAPI documentThe most common is to use Django-rest-framework, with Swagger use django-rest-swagger with python2.x, use drf-yasg with python3.xUse Flask-restplusSwagger and Redocs are included by default
Share the news now

Source : Viblo