跳到内容

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 地址,这样任何能与该机器通信的人都可以公开访问它。这通常是你在线上环境运行它的方式,例如,在容器中。

在大多数情况下,你会在上层使用一个“终止代理”(termination proxy)来为你处理 HTTPS,这取决于你如何部署你的应用,你的云服务提供商可能会为你做这件事,或者你可能需要自己设置。

提示

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