very long pause….
Ever arrived at a web application only to navigate away because the content you were looking for didn’t load soon enough?
According to TechEmpower, a platform that compares the performance of a wide range of web application frameworks using community-contributed test implementations. Rails performance - making it to place 363rd on the list - is way behind other popular backend framework alternatives like Flask coming in at number 360th or Django at 345th.
But there is a new player in town called FastApi leaving them all in the dust.
FastApi is a relatively new Python framework that enables you to rapidly develop an API or backend for your web or mobile application.
This one Framework is gaining popularity among Python developers because of its speed and usability.
Surpassing both Flask and Django’s performance, FastApi makes it to the 228th spot on TechEmpower’s list.
So what is FastApi bringing to the table that makes it that much faster?
Ideal for building high performance asyncio services with seriously impressive performance.
But the key to performance here is ASGI.
ASGI or Asynchronous Server Gateway Interface is a standard interface between async-capable Python web servers, frameworks, and applications.
It is essentially an asynchronous model for Python server communication.
Providing Python users with a framework that actually supports async and await.
When Python was created in the early 90’s it wasn’t designed with async support in mind because we simply didn’t have the computing power for it.
Without going into too much technical details, Pythons async problem lies in its use of a Global Interpreter Lock.
The global interpreter lock, or GIL, is a mutex that protects access to Python objects, preventing multiple threads from executing Python bytecodes at once.
This lock is necessary mainly because CPython’s memory management is not thread-safe. -PythonWiki
Python executes multi-threads by context switching even for idle processes creating a massive computing overhead.
Adding async support for Python didn’t come until asyncio was introduced with the Python 3.4 release in March, 2019.
And while Django and Flask both have a large following and provide very useful database tools, they both suffer when it comes to async and await support.
With Django claiming ASGI support while not yet supporting the await feature. Basically allowing you to do some asynchronous task but not being able to wait for the task to be done. And it’s the same thing with Flask.
Some of the new features from FastApi include:
Based on open standards
- OpenAPI for API creation, including declarations of path operations, parameters, body requests, security, etc.
- Automatic data model documentation with JSON Schema (as OpenAPI itself is based on JSON Schema).
- Designed around these standards, after a meticulous study. Instead of an afterthought layer on top.
- This also allows using automatic client code generation in many languages.
Interactive API documentation and exploration web user interfaces. As the framework is based on OpenAPI, there are multiple options, 2 included by default.
- Swagger UI, with interactive exploration, call and test your API directly from the browser.
Just Modern Python
It’s all based on standard Python 3.6 type declarations (thanks to Pydantic). No new syntax to learn. Just standard modern Python.
FastApi is cutting edge technology that you can use if you are building a badass application that will get tons of traffic.
And what’s great about it is that you don’t need to be a coding wizard to use it!
FastApi has project generation which include JWT token database authentication and rest API endpoints, and even built in admins allowing you to simply spin up a project and start using it.
So if you need Async and Await features or you are trying to gain some speed in your backend delivery be sure to check out this FastApi tutorial to get started!
Django and Flask are still very popular frameworks and I am sure we are going to continue using them for many, many years to come. However is alway good to keep an eye out for what’s new (and better in some ways) out there.
If you do try Starlette or FastApi I’d love to hear your thoughts on it!
Thank you so much for reading, and be sure to follow for similar programming content.