5個頂級異步Python框架
https://geekflare.com/python-asynchronous-web-frameworks/
async和await,此后,異步框架迅速發(fā)展了起來,性能上能和Node.js比肩,除非是CPU密集型任務(wù),否則沒有理由不使用異步框架。如果你是Web開發(fā)者,現(xiàn)在異步Web框架上有了更多選擇!
1、Tornado

協(xié)同程序和其他原語(tornado.gen,tornado.locks,tornado.queues等) 網(wǎng)絡(luò)模塊(tornado.ioloop,tornado.iostream) 異步服務(wù)器和客戶端(tornado.httpserver,httpclient,等)
通過這些模塊,Tornado 構(gòu)建了自己的異步Web框架模塊。
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()
2、 Snaic

async關(guān)鍵字之外,語法上和 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)
3、Vibora

Vibora聲稱比其它框架快幾倍,比競爭對手Sanic還快兩倍多。當然,這個基準測試要持懷疑態(tài)度。
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

from quart import Quart
app = Quart(__name__)
@app.route('/')
async def hello():
return 'hello'
app.run()
5、FastAPI

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}

總結(jié)
作者:劉志軍
_往期文章推薦_
評論
圖片
表情
