Search code examples
pythonasgi

migrating flask web application currently using uWSGI web server to ASGI web server(uvicorn)


I currently have a flask web application using uWSGI web server that implements the WSGI standard and need to migrate this app to uvicorn web server that implements the ASGI standard.

If I choose to use uvicorn web server from the many available options say, Hypercorn, Daphne, then which web microframework(instead of flask) should I opt for from the available options say, Starlette, Quart, Django/Channels to get this migration done smoothly?

The hierarchy is like:

  Uvicorn: an ASGI server 

        Starlette: (uses Uvicorn) a web microframework

             FastAPI: (uses Starlette) an API microframework with several
                      additional features for building APIs, with data validation, etc.

As what I have read so far,

Quart is a Python web microframework based on Asyncio. It is intended to provide the easiest way to use asyncio in a web context, especially with existing Flask apps.

and

FastAPI has shown to be a Python web framework with one of the best performances, as measured by third-party benchmarks, thanks to being based on and powered by Starlette. https://fastapi.tiangolo.com/benchmarks/

Please suggest with the best approach


Solution

  • So here I would like to add something that I have concluded so far,

    FastAPI learned from Flask (and several of its plug-ins) several things, including its simplicity. For example, the way you declare routes is very similar. That makes it easy to migrate from Flask to FastAPI (which I see a lot of people doing).

    Flask is a framework based on the current/old standard for Python web frameworks: WSGI.

    FastAPI is based on Starlette, which uses the newer standard for asynchronous web frameworks: ASGI.

    Starlette would be more comparable to Flask, in being a pure “micro-framework”. Almost everything that you can do with Flask, you can do with Starlette (and so with FastAPI). Nevertheless, Starlette has some features not available in Flask (nor in many other WSGI frameworks, like Django, at least by default), like WebSockets, background tasks and others.

    As FastAPI is based on Starlette, it inherits all its features. Including WebSockets, GraphQL support, templates, etc. So, at the minimum, with FastAPI you can do virtually everything you can with Flask.

    FastAPI is also a micro-framework (maybe mini-framework, as it includes some extra features for APIs). So, you can structure the project however you want, in many cases, you can even use most of the same files from a Flask project (I’ve done that).

    This has been explained in this amazing post: https://www.quora.com/What-are-the-advantages-of-using-FastAPI-over-flask

    Also, Docker image with Uvicorn managed by Gunicorn for high-performance FastAPI web applications in Python 3.7 and 3.6 with performance auto-tuning can be used for minimal implementation. https://github.com/tiangolo/uvicorn-gunicorn-fastapi-docker