Introducing 5 top asynchronous Python frameworks, come and learn quickly

Python introduced the asyncio library in 3.4, and added the keywords async and await in 3.6. Since then, the asynchronous framework has developed rapidly, and its performance is comparable to Node.js. Unless it is a CPU-intensive task, there is no reason not to use the asynchronous framework. .

If you are a web developer, you now have more choices when it comes to asynchronous web frameworks!

1、Tornado

Tornado is not a new framework at all. It was originally released in 2009 by FriendFeed (later acquired by Facebook). Asynchronous programming capabilities have been provided from the beginning.
Insert image description here

Tornado is not only a web framework, but also has many built-in asynchronous modules that can be used to build asynchronous applications yourself. These modules include:

  • Coroutines and other primitives (tornado.gen, tornado.locks, tornado.queues, etc.)
  • Network modules (tornado.ioloop, tornado.iostream)
  • Asynchronous servers and clients (tornado.httpserver, httpclient, etc.)

Through these modules, Tornado builds its own asynchronous web framework module.

import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")

def make_app():
    return tornado.web.Application([
        (r"/", MainHandler),
    ])

if __name__ == "__main__":
    app = make_app()
    app.listen(8888)
    tornado.ioloop.IOLoop.current().start()

Tornado has a large following in the Python community, and experienced architects use them to build powerful systems. This framework has been solving concurrency problems for a long time, but since it doesn't support the WSGI standard (most Python libraries are still in sync), it hasn't become mainstream yet.

It is said in China that Zhihu is built based on Torando.

2、 Snack

Sanic is a framework that has been born for 3 years: it only supports Python versions 3.6 and above, supports common async/await syntax, and works out of the box, so you can write an HTTP processor without reading a lot of documentation.
Insert image description here
Except for the async keyword, the syntax is the same as flask.

from sanic import Sanic
from sanic.response import json

app = Sanic()

@app.route("/")
async def test(request):
    return json({
    
    "hello": "world"})

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=8000)

Sanic is arguably the most popular and popular asynchronous framework in the Python world. It has all the features you need in your project: routing, middleware, cookies, version control, blueprints, class-based views, static files, streams, sockets, plus you can integrate templates, database drivers, file I/O, queues etc.

3、Vibora

Vibora, much like Sanic, aims to be the fastest Python web server. There is a frame comparison chart on the homepage of their website:

Insert image description here

Vibora claims to be several times faster than other frameworks and more than twice as fast as competitor Sanic. Of course, take this benchmark with a grain of salt.
Although Vibora is comparable to Sanic in terms of syntax and functionality I think Sanic is more mature as it has been around for a long time and has a larger community.

from vibora import Vibora, JsonResponse

app = Vibora()

@app.route('/')
async def home():
    return JsonResponse({
    
    'hello': 'world'})

if __name__ == '__main__':
    app.run(host="0.0.0.0", port=8000)

4、Quart

If you like Flask but want to support asynchronous support, then you will definitely like Quart
Insert image description here

Quart complies with the ASGI standard, the successor to the WSGI standard, and provides asynchronous support. Not only is Quart similar to Flask, it's also compatible with the Flask API! The authors of the framework want to keep the flavor of Flask and just add async, WebSocket and HTTP 2 support to it. Therefore, you can learn how to use Quart from the Flask documentation, just remember that the functions in Quart are asynchronous.

from quart import Quart

app = Quart(__name__)

@app.route('/')
async def hello():
    return 'hello'

app.run()

Almost exactly like Flask
Since Quart evolved from Flask, all the features of Flask are available: routing, middleware, sessions, templates, blueprints, etc. In fact, you can even use Flask extensions directly inside Quart. One problem, though, is that it only supports Python 3.7+.

5、FastAPI

FastAPI seems to be the most feature-rich and documented among the asynchronous Python frameworks.
Insert image description here

The authors of this framework delve into several other frameworks, from modern frameworks like Django to Sanic, as well as NestJS (Node.js, Typescript web framework).
The syntax could even be said to be more interesting than other frameworks we've come across:

rom fastapi import FastAPI

app = FastAPI()

@app.get("/users/me")
async def read_user_me():
    return {
    
    "user_id": "the current user"}

@app.get("/users/{user_id}")
async def read_user(user_id: str):
    return {
    
    "user_id": user_id}

FastAPI's trump card over other frameworks is that it automatically generates API documentation:
after writing the API interface, you can use standard-compliant UIs such as SwaggerUI, ReDoc, etc. to use the API.

Insert image description here

What about performance? FastAPI is built on the Starlette library and its performance matches Node and in some cases even Go. All in all, I really have a feeling that FastAPI is going to be the top async framework for Python.

Summarize

A lot is happening in the Python asynchronous ecosystem these days. New frameworks are emerging, old frameworks are being rewritten, and many libraries are beginning to support asynchronous behavior. For web frameworks, Python is ready to deliver great performance. If you have been thinking about migrating to Node or Go for a long time, you don’t need to now.

Before learning Python, I compiled some Python information and uploaded it to the CSDN official. If you have friends who want to learn other knowledge about Python, you can scan the QR code below to get it.

1. Python learning outline

The technical points in all directions of Python are organized to form a summary of knowledge points in various fields. Its usefulness is that you can find corresponding learning resources according to the above knowledge points to ensure that you learn more comprehensively.
Insert image description here

2. Essential development tools for Python

Insert image description here

3. Introductory learning video

Insert image description here

4. Practical cases

Optical theory is useless. You must learn to follow along and practice it in order to apply what you have learned to practice. At this time, you can learn from some practical cases.
Insert image description here

5. Python side job part-time and full-time routes

Insert image description here

6. Internet company interview questions

We must learn Python to find a high-paying job. The following interview questions are the latest interview materials from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and Alibaba bosses have given authoritative answers. After finishing this set I believe everyone can find a satisfactory job based on the interview information.
Insert image description here

Insert image description here

Guess you like

Origin blog.csdn.net/Z987421/article/details/132876466