|
1 |
| -# 手动运行服务器 - Uvicorn |
| 1 | +# 手动运行服务器 |
2 | 2 |
|
3 |
| -在远程服务器计算机上运行 **FastAPI** 应用程序所需的主要东西是 ASGI 服务器程序,例如 **Uvicorn**。 |
| 3 | +## 使用 `fastapi run` 命令 |
4 | 4 |
|
5 |
| -有 3 个主要可选方案: |
| 5 | +简而言之,使用 `fastapi run` 来运行您的 FastAPI 应用程序: |
6 | 6 |
|
7 |
| -* <a href="https://www.uvicorn.org/" class="external-link" target="_blank">Uvicorn</a>:高性能 ASGI 服务器。 |
8 |
| -* <a href="https://hypercorn.readthedocs.io/" class="external-link" target="_blank">Hypercorn</a>:与 HTTP/2 和 Trio 等兼容的 ASGI 服务器。 |
9 |
| -* <a href="https://github.com/django/daphne" class="external-link" target="_blank">Daphne</a>:为 Django Channels 构建的 ASGI 服务器。 |
10 |
| - |
11 |
| -## 服务器主机和服务器程序 |
12 |
| - |
13 |
| -关于名称,有一个小细节需要记住。 💡 |
14 |
| - |
15 |
| -“**服务器**”一词通常用于指远程/云计算机(物理机或虚拟机)以及在该计算机上运行的程序(例如 Uvicorn)。 |
| 7 | +<div class="termy"> |
16 | 8 |
|
17 |
| -请记住,当您一般读到“服务器”这个名词时,它可能指的是这两者之一。 |
| 9 | +```console |
| 10 | +$ <font color="#4E9A06">fastapi</font> run <u style="text-decoration-style:solid">main.py</u> |
18 | 11 |
|
19 |
| -当提到远程主机时,通常将其称为**服务器**,但也称为**机器**(machine)、**VM**(虚拟机)、**节点**。 这些都是指某种类型的远程计算机,通常运行 Linux,您可以在其中运行程序。 |
| 12 | + <span style="background-color:#009485"><font color="#D3D7CF"> FastAPI </font></span> Starting production server 🚀 |
20 | 13 |
|
| 14 | + Searching for package file structure from directories |
| 15 | + with <font color="#3465A4">__init__.py</font> files |
| 16 | + Importing from <font color="#75507B">/home/user/code/</font><font color="#AD7FA8">awesomeapp</font> |
21 | 17 |
|
22 |
| -## 安装服务器程序 |
| 18 | + <span style="background-color:#007166"><font color="#D3D7CF"> module </font></span> 🐍 main.py |
23 | 19 |
|
24 |
| -您可以使用以下命令安装 ASGI 兼容服务器: |
| 20 | + <span style="background-color:#007166"><font color="#D3D7CF"> code </font></span> Importing the FastAPI app object from the module with |
| 21 | + the following code: |
25 | 22 |
|
26 |
| -//// tab | Uvicorn |
| 23 | + <u style="text-decoration-style:solid">from </u><u style="text-decoration-style:solid"><b>main</b></u><u style="text-decoration-style:solid"> import </u><u style="text-decoration-style:solid"><b>app</b></u> |
27 | 24 |
|
28 |
| -* <a href="https://www.uvicorn.org/" class="external-link" target="_blank">Uvicorn</a>,一个快如闪电 ASGI 服务器,基于 uvloop 和 httptools 构建。 |
| 25 | + <span style="background-color:#007166"><font color="#D3D7CF"> app </font></span> Using import string: <font color="#3465A4">main:app</font> |
29 | 26 |
|
30 |
| -<div class="termy"> |
| 27 | + <span style="background-color:#007166"><font color="#D3D7CF"> server </font></span> Server started at <font color="#729FCF"><u style="text-decoration-style:solid">http://0.0.0.0:8000</u></font> |
| 28 | + <span style="background-color:#007166"><font color="#D3D7CF"> server </font></span> Documentation at <font color="#729FCF"><u style="text-decoration-style:solid">http://0.0.0.0:8000/docs</u></font> |
31 | 29 |
|
32 |
| -```console |
33 |
| -$ pip install "uvicorn[standard]" |
| 30 | + Logs: |
34 | 31 |
|
35 |
| ----> 100% |
| 32 | + <span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Started server process <b>[</b><font color="#34E2E2"><b>2306215</b></font><b>]</b> |
| 33 | + <span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Waiting for application startup. |
| 34 | + <span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Application startup complete. |
| 35 | + <span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Uvicorn running on <font color="#729FCF"><u style="text-decoration-style:solid">http://0.0.0.0:8000</u></font> <b>(</b>Press CTRL+C |
| 36 | + to quit<b>)</b> |
36 | 37 | ```
|
37 | 38 |
|
38 | 39 | </div>
|
39 | 40 |
|
40 |
| -/// tip |
| 41 | +这在大多数情况下都能正常运行。😎 |
41 | 42 |
|
42 |
| -通过添加`standard`,Uvicorn 将安装并使用一些推荐的额外依赖项。 |
| 43 | +例如,您可以使用该命令在容器、服务器等环境中启动您的 **FastAPI** 应用。 |
43 | 44 |
|
44 |
| -其中包括`uvloop`,它是`asyncio`的高性能替代品,它提供了巨大的并发性能提升。 |
| 45 | +## ASGI 服务器 |
45 | 46 |
|
46 |
| -/// |
| 47 | +让我们深入了解一些细节。 |
47 | 48 |
|
48 |
| -//// |
| 49 | +FastAPI 使用了一种用于构建 Python Web 框架和服务器的标准,称为 <abbr title="Asynchronous Server Gateway Interface,异步服务器网关接口">ASGI</abbr>。FastAPI 本质上是一个 ASGI Web 框架。 |
49 | 50 |
|
50 |
| -//// tab | Hypercorn |
| 51 | +要在远程服务器上运行 **FastAPI** 应用(或任何其他 ASGI 应用),您需要一个 ASGI 服务器程序,例如 **Uvicorn**。它是 `fastapi` 命令默认使用的 ASGI 服务器。 |
51 | 52 |
|
52 |
| -* <a href="https://github.com/pgjones/hypercorn" class="external-link" target="_blank">Hypercorn</a>,一个也与 HTTP/2 兼容的 ASGI 服务器。 |
53 |
| - |
54 |
| -<div class="termy"> |
| 53 | +除此之外,还有其他一些可选的 ASGI 服务器,例如: |
55 | 54 |
|
56 |
| -```console |
57 |
| -$ pip install hypercorn |
58 |
| - |
59 |
| ----> 100% |
60 |
| -``` |
61 |
| - |
62 |
| -</div> |
| 55 | +* <a href="https://www.uvicorn.org/" class="external-link" target="_blank">Uvicorn</a>:高性能 ASGI 服务器。 |
| 56 | +* <a href="https://hypercorn.readthedocs.io/" class="external-link" target="_blank">Hypercorn</a>:与 HTTP/2 和 Trio 等兼容的 ASGI 服务器。 |
| 57 | +* <a href="https://github.com/django/daphne" class="external-link" target="_blank">Daphne</a>:为 Django Channels 构建的 ASGI 服务器。 |
| 58 | +* <a href="https://github.com/emmett-framework/granian" class="external-link" target="_blank">Granian</a>:基于 Rust 的 HTTP 服务器,专为 Python 应用设计。 |
| 59 | +* <a href="https://unit.nginx.org/howto/fastapi/" class="external-link" target="_blank">NGINX Unit</a>:NGINX Unit 是一个轻量级且灵活的 Web 应用运行时环境。 |
63 | 60 |
|
64 |
| -...或任何其他 ASGI 服务器。 |
| 61 | +## 服务器主机和服务器程序 |
65 | 62 |
|
66 |
| -//// |
| 63 | +关于名称,有一个小细节需要记住。 💡 |
67 | 64 |
|
68 |
| -## 运行服务器程序 |
| 65 | +“**服务器**”一词通常用于指远程/云计算机(物理机或虚拟机)以及在该计算机上运行的程序(例如 Uvicorn)。 |
69 | 66 |
|
70 |
| -您可以按照之前教程中的相同方式运行应用程序,但不使用`--reload`选项,例如: |
| 67 | +请记住,当您一般读到“服务器”这个名词时,它可能指的是这两者之一。 |
71 | 68 |
|
72 |
| -//// tab | Uvicorn |
| 69 | +当提到远程主机时,通常将其称为**服务器**,但也称为**机器**(machine)、**VM**(虚拟机)、**节点**。 这些都是指某种类型的远程计算机,通常运行 Linux,您可以在其中运行程序。 |
73 | 70 |
|
74 |
| -<div class="termy"> |
75 | 71 |
|
76 |
| -```console |
77 |
| -$ uvicorn main:app --host 0.0.0.0 --port 80 |
| 72 | +## 安装服务器程序 |
78 | 73 |
|
79 |
| -<span style="color: green;">INFO</span>: Uvicorn running on http://0.0.0.0:80 (Press CTRL+C to quit) |
80 |
| -``` |
| 74 | +当您安装 FastAPI 时,它自带一个生产环境服务器——Uvicorn,并且您可以使用 `fastapi run` 命令来启动它。 |
81 | 75 |
|
82 |
| -</div> |
| 76 | +不过,您也可以手动安装 ASGI 服务器。 |
83 | 77 |
|
84 |
| -//// |
| 78 | +请确保您创建并激活一个[虚拟环境](../virtual-environments.md){.internal-link target=_blank},然后再安装服务器应用程序。 |
85 | 79 |
|
86 |
| -//// tab | Hypercorn |
| 80 | +例如,要安装 Uvicorn,可以运行以下命令: |
87 | 81 |
|
88 | 82 | <div class="termy">
|
89 | 83 |
|
90 | 84 | ```console
|
91 |
| -$ hypercorn main:app --bind 0.0.0.0:80 |
| 85 | +$ pip install "uvicorn[standard]" |
92 | 86 |
|
93 |
| -Running on 0.0.0.0:8080 over http (CTRL + C to quit) |
| 87 | +---> 100% |
94 | 88 | ```
|
95 | 89 |
|
96 | 90 | </div>
|
97 | 91 |
|
98 |
| -//// |
| 92 | +类似的流程也适用于任何其他 ASGI 服务器程序。 |
99 | 93 |
|
100 |
| -/// warning |
| 94 | +/// tip |
101 | 95 |
|
102 |
| -如果您正在使用`--reload`选项,请记住删除它。 |
| 96 | +通过添加 `standard` 选项,Uvicorn 将安装并使用一些推荐的额外依赖项。 |
103 | 97 |
|
104 |
| - `--reload` 选项消耗更多资源,并且更不稳定。 |
| 98 | +其中包括 `uvloop`,这是 `asyncio` 的高性能替代方案,能够显著提升并发性能。 |
105 | 99 |
|
106 |
| - 它在**开发**期间有很大帮助,但您**不应该**在**生产环境**中使用它。 |
| 100 | +当您使用 `pip install "fastapi[standard]"` 安装 FastAPI 时,实际上也会安装 `uvicorn[standard]`。 |
107 | 101 |
|
108 | 102 | ///
|
109 | 103 |
|
110 |
| -## Hypercorn with Trio |
111 |
| - |
112 |
| -Starlette 和 **FastAPI** 基于 <a href="https://anyio.readthedocs.io/en/stable/" class="external-link" target="_blank">AnyIO</a>, 所以它们才能同时与 Python 的标准库 <a href="https://docs.python.org/3/library/asyncio-task.html" class="external-link" target="_blank">asyncio</a> 和<a href="https://trio.readthedocs.io/en/stable/" class="external-link" target="_blank">Trio</a> 兼容。 |
113 |
| - |
114 |
| -尽管如此,Uvicorn 目前仅与 asyncio 兼容,并且通常使用 <a href="https://github.com/MagicStack/uvloop" class="external-link" target="_blank">`uvloop`</a >, 它是`asyncio`的高性能替代品。 |
115 |
| - |
116 |
| -但如果你想直接使用**Trio**,那么你可以使用**Hypercorn**,因为它支持它。 ✨ |
117 |
| - |
118 |
| -### 安装具有 Trio 的 Hypercorn |
| 104 | +## 运行服务器程序 |
119 | 105 |
|
120 |
| -首先,您需要安装具有 Trio 支持的 Hypercorn: |
| 106 | +如果您手动安装了 ASGI 服务器,通常需要以特定格式传递一个导入字符串,以便服务器能够正确导入您的 FastAPI 应用: |
121 | 107 |
|
122 | 108 | <div class="termy">
|
123 | 109 |
|
124 | 110 | ```console
|
125 |
| -$ pip install "hypercorn[trio]" |
126 |
| ----> 100% |
| 111 | +$ uvicorn main:app --host 0.0.0.0 --port 80 |
| 112 | + |
| 113 | +<span style="color: green;">INFO</span>: Uvicorn running on http://0.0.0.0:80 (Press CTRL+C to quit) |
127 | 114 | ```
|
128 | 115 |
|
129 | 116 | </div>
|
130 | 117 |
|
131 |
| -### Run with Trio |
| 118 | +/// note |
132 | 119 |
|
133 |
| -然后你可以传递值`trio`给命令行选项`--worker-class`: |
| 120 | +命令 `uvicorn main:app` 的含义如下: |
134 | 121 |
|
135 |
| -<div class="termy"> |
| 122 | +* `main`:指的是 `main.py` 文件(即 Python “模块”)。 |
| 123 | +* `app`:指的是 `main.py` 文件中通过 `app = FastAPI()` 创建的对象。 |
136 | 124 |
|
137 |
| -```console |
138 |
| -$ hypercorn main:app --worker-class trio |
| 125 | +它等价于以下导入语句: |
| 126 | + |
| 127 | +```Python |
| 128 | +from main import app |
139 | 129 | ```
|
140 | 130 |
|
141 |
| -</div> |
| 131 | +/// |
| 132 | + |
| 133 | +每种 ASGI 服务器程序通常都会有类似的命令,您可以在它们的官方文档中找到更多信息。 |
| 134 | + |
| 135 | +/// warning |
| 136 | + |
| 137 | +Uvicorn 和其他服务器支持 `--reload` 选项,该选项在开发过程中非常有用。 |
142 | 138 |
|
143 |
| -这将通过您的应用程序启动 Hypercorn,并使用 Trio 作为后端。 |
| 139 | +但 `--reload` 选项会消耗更多资源,且相对不稳定。 |
144 | 140 |
|
145 |
| -现在您可以在应用程序内部使用 Trio。 或者更好的是,您可以使用 AnyIO,使您的代码与 Trio 和 asyncio 兼容。 🎉 |
| 141 | +它对于**开发阶段**非常有帮助,但在**生产环境**中**不应该**使用。 |
| 142 | + |
| 143 | +/// |
146 | 144 |
|
147 | 145 | ## 部署概念
|
148 | 146 |
|
149 |
| -这些示例运行服务器程序(例如 Uvicorn),启动**单个进程**,在所有 IP(`0.0.0.0`)上监听预定义端口(例如`80`)。 |
| 147 | +这些示例运行服务器程序(例如 Uvicorn),启动**单个进程**,在所有 IP(`0.0.0.0`)上监听预定义端口(例如`80`)。 |
150 | 148 |
|
151 | 149 | 这是基本思路。 但您可能需要处理一些其他事情,例如:
|
152 | 150 |
|
153 | 151 | * 安全性 - HTTPS
|
154 | 152 | * 启动时运行
|
155 | 153 | * 重新启动
|
156 |
| -* Replication(运行的进程数) |
| 154 | +* 复制(运行的进程数) |
157 | 155 | * 内存
|
158 | 156 | * 开始前的步骤
|
159 | 157 |
|
|
0 commit comments