LeoMax
2 months ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with
41 additions and
41 deletions
-
docs/zh/docs/async.md
-
docs/zh/docs/python-types.md
-
docs/zh/docs/tutorial/body.md
-
docs/zh/docs/tutorial/index.md
-
docs/zh/docs/tutorial/path-params.md
|
@ -56,7 +56,7 @@ def results(): |
|
|
|
|
|
|
|
|
## 技术细节 |
|
|
## 技术细节 |
|
|
|
|
|
|
|
|
Python 的现代版本支持通过一种叫**"协程"**——使用 `async` 和 `await` 语法的东西来写**”异步代码“**。 |
|
|
Python 的现代版本支持通过一种叫 **"协程"** ——使用 `async` 和 `await` 语法的东西来写 **"异步代码"**。 |
|
|
|
|
|
|
|
|
让我们在下面的部分中逐一介绍: |
|
|
让我们在下面的部分中逐一介绍: |
|
|
|
|
|
|
|
@ -415,7 +415,7 @@ Starlette (和 **FastAPI**) 是基于 <a href="https://anyio.readthedocs.io/ |
|
|
|
|
|
|
|
|
### 路径操作函数 |
|
|
### 路径操作函数 |
|
|
|
|
|
|
|
|
当你使用 `def` 而不是 `async def` 来声明一个*路径操作函数*时,它运行在外部的线程池中并等待其结果,而不是直接调用(因为它会阻塞服务器)。 |
|
|
当你使用 `def` 而不是 `async def` 来声明一个 **路径操作函数** 时,它运行在外部的线程池中并等待其结果,而不是直接调用(因为它会阻塞服务器)。 |
|
|
|
|
|
|
|
|
如果你使用过另一个不以上述方式工作的异步框架,并且你习惯于用普通的 `def` 定义普通的仅计算路径操作函数,以获得微小的性能增益(大约100纳秒),请注意,在 FastAPI 中,效果将完全相反。在这些情况下,最好使用 `async def`,除非路径操作函数内使用执行阻塞 <abbr title="输入/输出:磁盘读写,网络通讯.">I/O</abbr> 的代码。 |
|
|
如果你使用过另一个不以上述方式工作的异步框架,并且你习惯于用普通的 `def` 定义普通的仅计算路径操作函数,以获得微小的性能增益(大约100纳秒),请注意,在 FastAPI 中,效果将完全相反。在这些情况下,最好使用 `async def`,除非路径操作函数内使用执行阻塞 <abbr title="输入/输出:磁盘读写,网络通讯.">I/O</abbr> 的代码。 |
|
|
|
|
|
|
|
|
|
@ -230,7 +230,7 @@ John Doe |
|
|
|
|
|
|
|
|
<a href="https://docs.pydantic.dev/" class="external-link" target="_blank">Pydantic</a> 是一个用来用来执行数据校验的 Python 库。 |
|
|
<a href="https://docs.pydantic.dev/" class="external-link" target="_blank">Pydantic</a> 是一个用来用来执行数据校验的 Python 库。 |
|
|
|
|
|
|
|
|
你可以将数据的"结构"声明为具有属性的类。 |
|
|
你可以将数据的 **"结构"** 声明为具有属性的类。 |
|
|
|
|
|
|
|
|
每个属性都拥有类型。 |
|
|
每个属性都拥有类型。 |
|
|
|
|
|
|
|
|
|
@ -2,7 +2,7 @@ |
|
|
|
|
|
|
|
|
FastAPI 使用 **请求体** 从客户端(例如浏览器)向 API 发送数据。 |
|
|
FastAPI 使用 **请求体** 从客户端(例如浏览器)向 API 发送数据。 |
|
|
|
|
|
|
|
|
**请求体**是客户端发送给 API 的数据。**响应体**是 API 发送给客户端的数据。 |
|
|
**请求体** 是客户端发送给 API 的数据。**响应体** 是 `API` 发送给客户端的数据。 |
|
|
|
|
|
|
|
|
API 基本上肯定要发送 **响应体**,但是客户端不一定发送 **请求体**。 |
|
|
API 基本上肯定要发送 **响应体**,但是客户端不一定发送 **请求体**。 |
|
|
|
|
|
|
|
|
|
@ -58,13 +58,13 @@ $ pip install "fastapi[all]" |
|
|
|
|
|
|
|
|
假如你想将应用程序部署到生产环境,你可能要执行以下操作: |
|
|
假如你想将应用程序部署到生产环境,你可能要执行以下操作: |
|
|
|
|
|
|
|
|
``` |
|
|
``` shell |
|
|
pip install fastapi |
|
|
pip install fastapi |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
并且安装`uvicorn`来作为服务器: |
|
|
并且安装`uvicorn`来作为服务器: |
|
|
|
|
|
|
|
|
``` |
|
|
``` shell |
|
|
pip install "uvicorn[standard]" |
|
|
pip install "uvicorn[standard]" |
|
|
``` |
|
|
``` |
|
|
|
|
|
|
|
|
|
@ -149,7 +149,7 @@ Python 3.4 及之后版本支持<a href="https://docs.python.org/zh-cn/3/library |
|
|
|
|
|
|
|
|
### 声明 *路径参数* |
|
|
### 声明 *路径参数* |
|
|
|
|
|
|
|
|
使用 Enum 类(`ModelName`)创建使用类型注解的*路径参数*: |
|
|
使用 `Enum` 类(`ModelName`)创建使用类型注解的 *路径参数*: |
|
|
|
|
|
|
|
|
{* ../../docs_src/path_params/tutorial005.py hl[16] *} |
|
|
{* ../../docs_src/path_params/tutorial005.py hl[16] *} |
|
|
|
|
|
|
|
|