從 Flask 到 FastAPI 的平滑遷移

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"本文最初發佈於 BetterProgramming,經原作者授權由 InfoQ 中文站翻譯並分享。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"本文將介紹 FastAPI 背後的基本概念,以及將 Flask 服務器過渡到 FastAPI 服務器所涉及的步驟和代碼對比以供參考。根據 官方文檔,FastAPI 的框架是:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"… 一個現代、快速(高性能)的 Web 框架,基於標準 Python 類型提示,使用 Python 3.6+ 構建 API。"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"衆所周知,Flask 是百分百 WSGI(Web Server Gateway Interface,Web 服務器網關接口)的微型 web 框架。隨着發展,ASGI(Asynchronous Server Gateway Interface,異步服務器網關接口)作爲 WSGI 精神的繼承者,實現了在 I\/O 綁定的語境下的高吞吐量,支持 HTTP\/2 以及 WebSockets,這些都是 WSGI 所不能及的。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"隨着科技的發展,快如閃電的 ASGI 服務器 Uvicorn 誕生了。然而,Uvicorn 也僅僅只是一個不具備任何路由功能的 web 服務器。Starlette 的出現則是在 ASGI 的服務器(Uvicorn、Daphne,以及 Hypercorn)的基礎上提供了一套完整的 ASGI 工具箱。如果要說這二者有什麼直接的區別,Starlette 是 ASGI 的 web 框架,而 Flask 則是 WSGI 的 web 框架。"}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章