Change language

Why you should choose the FastAPI framework for your next project

|

FastAPI is a framework for building concise and fairly fast HTTP API servers with built-in validation, serialisation and asynchrony. It stands on the shoulders of two other frameworks. The web handling in FastAPI is done by Starlette, validation is done by Pydantic.

The combine is light, not overloaded and more than enough functionality.

Starlette is a new, fast and cool framework which implements the ASGI approach. It's all about asynchronous and the new features of the 3rd branch of Python. Moreover Starlette has a whole bunch of serious stuff:

  • GraphQL out of the box.
  • Web sockets already built in and ready to go
  • Complete set of middleware for authorization/authentication, CORS
  • Built-in asynchronous tasking

Asynchronous programming is software/user-space threading, where the application, not the CPU, handles threading and context switching. In asynchronous programming, context switches only at specified switching points, not at intervals defined by the CPU.

When you run something asynchronously, it means that it is not blocked, you execute it without waiting for it to complete, and you continue to do other things. Parallelism means running multiple tasks simultaneously, in parallel. Parallelism works well when you can separate tasks into independent pieces of work.

Basic FastAPI features

FastAPI is essentially a slap on the native Starlette classes, adding a bunch of new features to an already good framework.

Nice little features for creating REST API services + Swagger documentation for methods. Starlette focuses on trendy GraphQL, FastAPI takes care of those who write REST.

Handy bells and whistles built on variable type hints. For example, built-in data validators.

Nice utilities for authorisation and authentication processes - JWT, OAuth2 support.

Пример простого приложения на FastAPI

Important reasons for choosing FastAPI:

  • Asynchronous
  • Typing
  • Embedded documentation (Swagger)
  • Usability

When to use FastAPI

  • When your project needs performance and needs it to be faster than other frameworks - e.g. Django.
  • When your project needs asynchrony.
  • If you use requests to other services in your project (using asynchronous http requests).
  • If you want to use websockets out of the box.

If you answered "no" to most of these questions, you might want to consider other frameworks or use FastAPI. The choice is up to you.

See also FastAPI documentation

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically