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

Question:

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

Asked By: Khushboo Mulani

||

Answers:

I’m not sure there is a right answer for this, as it mainly depends on personal opinions.

I personally believe that Quart would provide the easiest migration from an existing Flask app. This is because I’ve intentionally made the Quart API the same as the Flask API thereby ensuring that everything you already understand about Flask apps still applies to Quart apps. My hope is that this would allow you to focus on learning async/await concepts instead of the framework.

I think you’ll find your answer though by considering what you need to do that is beyond the framework and whether extensions exist for you to be able to do it. Compared to the Flask ecosystem all the ASGI frameworks’ ecosystems are smaller.

Answered By: pgjones

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

Answered By: Khushboo Mulani

FastAPI is not comparable to Flask. It’s comparable to something like Flask-RESTPlus.

Starlette is a fair comparison with Flask. Starlette is more up to date with modern standards, similar to Quart, Sanic, Bocadillo, etc.

As for the best approach… it’s simply toil to migrate an app from one framework to another.
I would stick with Gunicorn either way (using the Uvicorn worker class), since the FastAPI author recommends to do so.

Answered By: Vasili Syrakis
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.