Petty Little Fun # 2: Starlette

    Tunnel vision

    It just so happened that many web applications are written in Python. This niche of Python development was almost completely shared between two healthy players - Django and Flask. Therefore, a large percentage of Python programmers are dedicated to working with these two frameworks.

    For this reason, many Python developers have some kind of tunnel vision - their engineering approach is locked between the two libraries.

    Some programmers are not limited to Djano and Flask and add all sorts of new things to their combat tools. For example, the fashionable Sanic framework .

    Tectonic shift: from WSGI to ASGI

    During the period of rapid adaptation of Python to the needs of web development, the community came up with the WSGI standard - Web Server Gateway Interface. This protocol described how a web server could send HTTP requests for processing in a Python application and receive responses from there.

    WSGI paved the way for the development of many web development frameworks and libraries. They were all different in their architecture, but the same in their way of communicating with an external web server. WSGI was introduced by the community as far back as 2003, and all the popular classic pet-powered web frameworks (including Django and Flask) still support it.

    Problems with WSGI began after powerful tools for asynchronously executing code and coroutines appeared in the Python core. WSGI is old and not at all focused on working with new language features. Therefore, there was a need for a new, asynchronous protocol for communicating a web server with Python programs. So ASGI (Asynchronous Server Gateway Interface) appeared - an ideological descendant of WSGI , but with coroutines and asynchrony.

    The developers of the old frameworks were held hostage by their audience - they simply cannot take and transfer their frameworks to the asynchronous approach (this will break the code and destroy compatibility), so all development using ASGI was concentrated in new frameworks released in the last couple of years, and Django

    Starlette - a brilliant framework

    Starlette  is a new, nimble and cool framework that implements the ASGI approach. Everything about it is focused on asynchrony and new chips of the 3rd Python branch.

    In addition, Starlette has a whole bunch of serious goodies.

    • GraphQL out of the box. Yes, this new approach to developing client-server interactions begins to push REST and takes its place in the world of web frameworks.
    • Web sockets are already built-in and ready to go.
    • Ready-made set of middleware for working with authorization / authentication, CORS.
    • Built-in asynchronous tasks.

    Solid lotion - FastAPI

    Some Starlette programmers wildly liked it and they created an extension for this framework - FastAPI

    FastAPI is, in fact, a blotch on the native Starlette classes , adding a bunch of new features to the already good framework.

    • Buns for creating REST API services + Swagger documentation for methods. Starlette focuses on trendy GraphQL , FastAPI takes care of those who saw REST .
    • Convenient gadgets built on tooltips-types of variables. For example, built-in data validators.
    • Nice utility for authentication and authentication processes - support for JWT, OAuth2.

    And a number of small amenities and amenities.

    In the dry residue

    It's time to dive into the world of ASGI and its frameworks (unless, of course, you have already done so). Asynchronous solutions are still far from dominating the market, but they are actively advancing. And first of all - because of its speed.

    Also popular now: