在python中使用fastapi入门

本文介绍了使用Python构建REST API的常用框架Flask和Django,并指出FastAPI作为基于标准Python类型提示的高性能Web框架,其优势在于异步处理。文章详细展示了如何安装FastAPI和uvicorn,以及如何创建基本的FastAPI程序,指定路径、查询参数、请求正文、表格数据和文件上传。FastAPI还支持自动生成Swagger API文档,简化API文档编写。
摘要由CSDN通过智能技术生成

The most commonly used web frameworks for building REST APIs with python is Flask and Django. Though we could build APIs with django, the real purpose of Django is building an end to end web application. On the other hand, flask is pretty light weight and can help us quickly build REST APIs. Before jumping into fastapi let’s quickly revisit a basic flask application.

使用python构建REST API的最常用Web框架是Flask和Django。 尽管我们可以使用django构建API,但是Django的真正目的是构建端到端的Web应用程序。 另一方面,flask重量很轻,可以帮助我们快速构建REST API。 在进入fastapi之前,让我们快速回顾一下基本的flask应用程序。

from flask import Flask, jsonify

app = Flask(__name__)


@app.route("/", methods = ["GET"])
def index():
return jsonify({"appname" :"firstapp"}),200


if __name__ == "__main__":
app.run(debug=True)

Running the above code should give the following logs and output

运行上面的代码应提供以下日志和输出

* Serving Flask app "myflaskapi" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 207-495-435
127.0.0.1 - - [25/Sep/2020 12:35:03] "GET / HTTP/1.1" 200 -Output:
{
"appname": "firstapp"
}

The fundamental problem of flask is it is based on WSGI server which is synchronous. This means that these cannot perform any non blocking asynchronous operations. For instance , consider the example below.

flask的根本问题是它基于同步的WSGI服务器。 这意味着它们不能执行任何非阻塞异步操作。 例如,请考虑以下示例。

@app.route("/", methods=["GET"])
def index():
response = requests.get(url="https://google.com")
print(response.content)
return jsonify({"content" : str(response.content)}), 200

In the above code, the response is returned only after the execution of requests.get method. Until then, the API is blocked and unavailable to handle further requests.

在上面的代码中,仅在执行requests.get方法之后返回响应。 在此之前,该API被阻止并且无法处理其他请求。

This use case is still valid with a WSGI server like gunicorn handling the requests with worker threads. Ofcourse one would argue that this could still be made asynchronous using Klein or other libraries. But that still adds further overhead.

这种用例对于像gunicorn这样的WSGI服务器仍然有效,该服务器使用工作线程处理请求。 当然有人会说,仍然可以使用Klein或其他库将其异步化。 但这仍然增加了额外的开销。

We also cannot use the asyncio library with flask apis as asyncio expects a coroutine and flask is incompatible with a coroutine.

我们也不能将asyncio库与flask api一起使用,因为asyncio期望coroutine并且flask与协程不兼容。

To get around this, we have fastapi which is based on asyncio.

为了解决这个问题,我

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值