跳到内容

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:solid">main.py</u>

  <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 的命令行程序就是 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,这取决于您如何部署应用程序,您的提供商可能会为您完成此操作,或者您可能需要自行设置。

提示

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