跳到内容

FastAPI CLI

FastAPI CLI 是一个命令行程序,你可以用它来运行 FastAPI 应用、管理 FastAPI 项目等。

当你安装 FastAPI 时(例如通过 pip install "fastapi[standard]"),它会附带一个可以在终端中运行的命令行程序。

若要为开发环境运行 FastAPI 应用,可以使用 fastapi dev 命令。

$ <font color="#4E9A06">fastapi</font> dev

  <span style="background-color:#009485"><font color="#D3D7CF"> FastAPI </font></span>  Starting development server 🚀

             Searching for package file structure from directories with
             <font color="#3465A4">__init__.py</font> files
             Importing from <font color="#75507B">/home/user/code/</font><font color="#AD7FA8">awesomeapp</font>

   <span style="background-color:#007166"><font color="#D3D7CF"> module </font></span>  🐍 main.py

     <span style="background-color:#007166"><font color="#D3D7CF"> code </font></span>  Importing the FastAPI app object from the module with the
             following code:

             <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>

      <span style="background-color:#007166"><font color="#D3D7CF"> app </font></span>  Using import string: <font color="#3465A4">main:app</font>

   <span style="background-color:#007166"><font color="#D3D7CF"> server </font></span>  Server started at <font color="#729FCF"><u style="text-decoration-style:solid">http://127.0.0.1:8000</u></font>
   <span style="background-color:#007166"><font color="#D3D7CF"> server </font></span>  Documentation at <font color="#729FCF"><u style="text-decoration-style:solid">http://127.0.0.1:8000/docs</u></font>

      <span style="background-color:#007166"><font color="#D3D7CF"> tip </font></span>  Running in development mode, for production use:
             <b>fastapi run</b>

             Logs:

     <span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span>  Will watch for changes in these directories:
             <b>[</b><font color="#4E9A06">&apos;/home/user/code/awesomeapp&apos;</font><b>]</b>
     <span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span>  Uvicorn running on <font color="#729FCF"><u style="text-decoration-style:solid">http://127.0.0.1:8000</u></font> <b>(</b>Press CTRL+C to
             quit<b>)</b>
     <span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span>  Started reloader process <b>[</b><font color="#34E2E2"><b>383138</b></font><b>]</b> using WatchFiles
     <span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span>  Started server process <b>[</b><font color="#34E2E2"><b>383153</b></font><b>]</b>
     <span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span>  Waiting for application startup.
     <span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span>  Application startup complete.

提示

对于生产环境,你应该使用 fastapi run 而不是 fastapi dev。🚀

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

fastapi CLI 会尝试自动检测要运行的 FastAPI 应用,默认假设该应用是一个名为 app 的对象,位于 main.py 文件中(或其他几种变体)。

但你也可以显式地配置要使用的应用。

pyproject.toml 中配置应用 entrypoint

你可以在 pyproject.toml 文件中配置应用的位置,如下所示:

[tool.fastapi]
entrypoint = "main:app"

entrypoint 将告诉 fastapi 命令以如下方式导入应用:

from main import app

如果你的代码结构如下:

.
├── backend
│   ├── main.py
│   ├── __init__.py

那么你可以将 entrypoint 设置为:

[tool.fastapi]
entrypoint = "backend.main:app"

这等同于:

from backend.main import app

带有路径的 fastapi dev

你也可以将文件路径传递给 fastapi dev 命令,它会自动推测要使用的 FastAPI 应用对象:

$ fastapi dev main.py

但你必须记得在每次调用 fastapi 命令时都传递正确的路径。

此外,其他工具(例如 VS Code 扩展FastAPI Cloud)可能无法找到应用,因此建议在 pyproject.toml 中使用 entrypoint 进行配置。

fastapi dev

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

默认情况下,自动重载 (auto-reload) 已启用,当你修改代码时,服务器会自动重新加载。这非常消耗资源,并且可能比禁用时更不稳定。你应该仅在开发时使用它。它还会监听 IP 地址 127.0.0.1,这是你的机器仅与自身通信的 IP(localhost)。

fastapi run

执行 fastapi run 会以生产模式启动 FastAPI。

默认情况下,自动重载处于禁用状态。它会监听 IP 地址 0.0.0.0,这意味着它会监听所有可用的 IP 地址,从而使任何能够与该机器通信的人都可以访问它。这通常是你在线上环境(例如在容器中)运行它的方式。

在大多数情况下,你将会(也应该)在顶层有一个“终止代理 (termination proxy)”来为你处理 HTTPS。这取决于你如何部署应用程序:你的提供商可能会为你完成这项工作,或者你可能需要自己设置它。

提示

你可以在 部署文档 中了解更多相关信息。