FastAPI - 一款新型的 Python Web 框架(对比 Flask)

近日只是为了想尽办法为 Flask 实现 Swagger UI 文档功能,基本上要让Flask配合 Flasgger, 所以写了篇Flask 应用集成 Swagger UI 。然而不断的 Google 过程中偶然间发现了FastAPI这么一款集成了 Swagger UI 的更新的 Python Web 框架。起初想要在标题中表达的意思大概是 Flask + Swagger = FastAPI, 后来发现 FastAPI 的闪亮点不仅如此,顺便找了些 Flask 与 FastAPI 对比的文章来,在文后附有链接。

本文不对 Flask 与 FastAPI 的各个方面对进行对比,本人兴趣依然还在 FastAPI 的 Swagger UI 功能,以及与 Flask 的 Blueprint 类似的特性。如果要拿 Flask 与 FastAPI 比较的话,应该用 Flask 2.x, 因为它开始支持类似 @app.get 的装饰器,并引入了 async 路由函数。

Flask 是在 2010 年发布的,它构建于 WSGI(Python Web Server Gateway Interface) 之上的,产品环境中运行需与 uWSGI, Gunicorn 搭配,或用 mod_wsgi 模块与 Apache 集成。因发布较早,所以目前应该有较多的使用者。Flask 2.0 需要 Python 3.6+ 的支持,如果支持 , 需 Python 3.7+async

FastAPI 发布于 2018 年,构建于 ASGI(Asynchronous Server Gateway Interface) 之上,在 IO 密集型的应用中有更优越的性能。生成环境中配合 ASGI 服务器,如UvicornHypercorn .FastAPI 最为亮丽的特性是集成了 Swagger UI -- 外加一个福利ReDoc 。FastAPI 需 Python 3.6+ 版本。

毕竟是在开始学一个新的框架,还是从它的基本用法开始,途中会穿插与 Flask 的对比。

FastAPI 的基本用法

安装:

<span style="color:#444444"><span style="background-color:#f6f6f6"> $ pip <span style="color:#333333"><strong>install</strong></span> fastapi  $ pip <span style="color:#333333"><strong>install</strong></span> <span style="color:#880000">"uvicorn[standard]"</span> 
</span></span>

当前安装的 fastapi 版本为 0.70.1, uvicorn 版本为 0.16.0。开始第一个例子,摘自官方

创建一个 文件,内容为main.py

<span style="color:#333333"><span style="background-color:#f6f6f6"><span style="color:#444444"><span style="background-color:#f6f6f6"><span style="color:#333333"><strong>from</strong></span> typing <span style="color:#333333"><strong>import</strong></span> Optional
<span style="color:#333333"><strong>from</strong></span> fastapi <span style="color:#333333"><strong>import</strong></span> FastAPI

app = FastAPI()


<span style="color:#1f7199">@app.get("/")</span>
<span style="color:#333333"><strong>def</strong></span> <span style="color:#880000"><strong>read_root</strong></span>():
    <span style="color:#333333"><strong>return</strong></span> {<span style="color:#880000">"Hello"</span>: <span style="color:#880000">"World"</span>}


<span style="color:#1f7199">@app.get("/items/{item_id}")</span>
<span style="color:#333333"><strong>def</strong></span> <span style="color:#880000"><strong>read_item</strong></span>(item_id: int, q: Optional[str] = None):
    <span style="color:#333333"><strong>return</strong></span> {<span style="color:#880000">"item_id"</span>: item_id, <span style="color:#880000">"q"</span>: q}</span></span></span></span>

从键入导入可选

从 fastapi 导入 FastAPI

应用 = 快速 API ( )

@ 应用 . 获取 ( "/" )

防御read_root ( ) :

     返回 { "Hello" : "World" }

@ app . get ( "/items/{item_id}" )

防御read_item (item_id : int , q : 可选 [ str ] = 无 ) :

     返回{ "item_id" : item_id , "q" : q }

注:以上两个函数前面可以加上 关键字async

启动服务,用命令

<span style="color:#444444"><span style="background-color:#f6f6f6"> $  uvicorn main:app --reload  INFO: Will watch <span style="color:#333333"><strong>for</strong></span> <span style="color:#333333"><strong>changes</strong></span> in these directorie<span style="color:#bc6060">s:</span> [<span style="color:#880000">'/Users/yanbin/demo/first-fastapi'</span>]  INFO: Uvicorn running <span style="color:#333333"><strong>on</strong></span> http://<span style="color:#880000">127.0</span>.<span style="color:#880000">0.1</span>:<span style="color:#880000">8000</span> (Press CTRL+C <span style="color:#333333"><strong>to</strong></span> <span style="color:#333333"><strong>quit</strong></span>)  INFO: Started reloader process [<span style="color:#880000">81730</span>] using watchgod  INFO: Started server process [<span style="color:#880000">81732</
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值