Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 352
Khushboo Mulani Avatar asked Feb 18 '20 11:02

Khushboo Mulani


People also ask

Can I use Uvicorn with flask?

Flask is a Python-based microframework that is popular with web developers, given its lightweight nature and ease of use. This tutorial will focus on deploying a Flask app to App Platform using gunicorn. Gunicorn is a Python WSGI HTTP Server that uses a pre-fork worker model.

Does flask support ASGI?

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.

Is Uvicorn a Web server?

Introduction. Uvicorn is an ASGI web server implementation for Python. Until recently Python has lacked a minimal low-level server/application interface for async frameworks. The ASGI specification fills this gap, and means we're now able to start building a common set of tooling usable across all async frameworks.

What is the use of Uvicorn?

uvicorn is an ASGI (async server gateway interface) compatible web server. It's (simplified) the binding element that handles the web connections from the browser or api client and then allows FastAPI to serve the actual request.


1 Answers

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

like image 166
Khushboo Mulani Avatar answered Nov 12 '22 09:11

Khushboo Mulani