跳至内容

FastAPI CLI

FastAPI CLI 是一个命令行程序,您可以使用它来服务您的 FastAPI 应用程序、管理您的 FastAPI 项目等等。

当您安装 FastAPI(例如,使用 pip install "fastapi[standard]")时,它包含一个名为 fastapi-cli 的包,此包在终端中提供 fastapi 命令。

要运行您的 FastAPI 应用程序进行开发,您可以使用 fastapi dev 命令

$ <font color="#4E9A06">fastapi</font> dev <u style="text-decoration-style:single">main.py</u>
<font color="#3465A4">INFO    </font> Using path <font color="#3465A4">main.py</font>
<font color="#3465A4">INFO    </font> Resolved absolute path <font color="#75507B">/home/user/code/awesomeapp/</font><font color="#AD7FA8">main.py</font>
<font color="#3465A4">INFO    </font> Searching for package file structure from directories with <font color="#3465A4">__init__.py</font> files
<font color="#3465A4">INFO    </font> Importing from <font color="#75507B">/home/user/code/</font><font color="#AD7FA8">awesomeapp</font>

 ╭─ <font color="#8AE234"><b>Python module file</b></font> ─╮
 │                      │
 │  🐍 main.py          │
 │                      │
 ╰──────────────────────╯

<font color="#3465A4">INFO    </font> Importing module <font color="#4E9A06">main</font>
<font color="#3465A4">INFO    </font> Found importable FastAPI app

 ╭─ <font color="#8AE234"><b>Importable FastAPI app</b></font> ─╮
 │                          │
 │  <span style="background-color:#272822"><font color="#FF4689">from</font></span><span style="background-color:#272822"><font color="#F8F8F2"> main </font></span><span style="background-color:#272822"><font color="#FF4689">import</font></span><span style="background-color:#272822"><font color="#F8F8F2"> app</font></span><span style="background-color:#272822">  </span>  │
 │                          │
 ╰──────────────────────────╯

<font color="#3465A4">INFO    </font> Using import string <font color="#8AE234"><b>main:app</b></font>

 <span style="background-color:#C4A000"><font color="#2E3436">╭────────── FastAPI CLI - Development mode ───────────╮</font></span>
 <span style="background-color:#C4A000"><font color="#2E3436">│                                                     │</font></span>
 <span style="background-color:#C4A000"><font color="#2E3436">│  Serving at: http://127.0.0.1:8000                  │</font></span>
 <span style="background-color:#C4A000"><font color="#2E3436">│                                                     │</font></span>
 <span style="background-color:#C4A000"><font color="#2E3436">│  API docs: http://127.0.0.1:8000/docs               │</font></span>
 <span style="background-color:#C4A000"><font color="#2E3436">│                                                     │</font></span>
 <span style="background-color:#C4A000"><font color="#2E3436">│  Running in development mode, for production use:   │</font></span>
 <span style="background-color:#C4A000"><font color="#2E3436">│                                                     │</font></span>
 <span style="background-color:#C4A000"><font color="#2E3436">│  </font></span><span style="background-color:#C4A000"><font color="#555753"><b>fastapi run</b></font></span><span style="background-color:#C4A000"><font color="#2E3436">                                        │</font></span>
 <span style="background-color:#C4A000"><font color="#2E3436">│                                                     │</font></span>
 <span style="background-color:#C4A000"><font color="#2E3436">╰─────────────────────────────────────────────────────╯</font></span>

<font color="#4E9A06">INFO</font>:     Will watch for changes in these directories: [&apos;/home/user/code/awesomeapp&apos;]
<font color="#4E9A06">INFO</font>:     Uvicorn running on <b>http://127.0.0.1:8000</b> (Press CTRL+C to quit)
<font color="#4E9A06">INFO</font>:     Started reloader process [<font color="#34E2E2"><b>2265862</b></font>] using <font color="#34E2E2"><b>WatchFiles</b></font>
<font color="#4E9A06">INFO</font>:     Started server process [<font color="#06989A">2265873</font>]
<font color="#4E9A06">INFO</font>:     Waiting for application startup.
<font color="#4E9A06">INFO</font>:     Application startup complete.

名为 fastapi 的命令行程序是 FastAPI CLI

FastAPI CLI 获取您 Python 程序的路径(例如 main.py)并自动检测 FastAPI 实例(通常命名为 app),确定正确的导入过程,然后对其进行服务。

对于生产环境,您将改为使用 fastapi run。 🚀

在内部,FastAPI CLI 使用 Uvicorn,一个高性能、生产就绪的 ASGI 服务器。 😎

fastapi dev

运行 fastapi dev 会启动开发模式。

默认情况下,自动重新加载已启用,当您更改代码时,会自动重新加载服务器。这会消耗大量资源,并且可能不如禁用时稳定。您应该只在开发过程中使用它。它还在 IP 地址 127.0.0.1 上侦听,这是您的机器仅与其自身通信的 IP(localhost)。

fastapi run

执行 fastapi run 默认情况下会以生产模式启动 FastAPI。

默认情况下,自动重新加载已禁用。它还在 IP 地址 0.0.0.0 上侦听,这意味着所有可用的 IP 地址,这样它将对任何可以与机器通信的人公开访问。这通常是您在生产环境中运行它的方式,例如,在容器中。

在大多数情况下,您(并且应该)会拥有一个“终止代理”为您处理 HTTPS,这将取决于您如何部署应用程序,您的提供商可能会为您执行此操作,或者您可能需要自己设置它。

提示

您可以在 部署文档 中了解更多信息。