从 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 框架。"}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章