FastAPI CLI

在这里插入图片描述

FastAPI CLI

  FastAPI CLI是一个强大的命令行接口工具,‌用于简化FastAPI项目的创建、‌管理和部署过程。‌它提供了如项目结构管理、‌可选的Dockerfile和docker-compose文件生成等功能,‌帮助开发者快速启动和迭代FastAPI项目。‌此外,‌FastAPI CLI还支持热重载和即时代码更新,‌非常适合开发过程中的快速迭代和实时测试。‌使用FastAPI CLI可以显著提高开发效率,‌是FastAPI框架不可或缺的一部分。‌你可以用它来部署和运行你的 FastAPI 应用程序,管理你的 FastAPI 项目,等等。

  当你安装 FastAPI 时(例如使用 pip install FastAPI 命令),会包含一个名为 fastapi-cli 的软件包,该软件包在终端中提供 fastapi 命令。

1.fastapi dev

当你运行 fastapi dev时,它将以开发模式运行。

(fastapi_env) PS E:> fastapi dev
INFO     Using path main.py
INFO     Resolved absolute path E:\PyCharmProject\FastApiProject\fastApiProject\main.py
INFO     Searching for package file structure from directories with __init__.py files
INFO     Importing from E:\PyCharmProject\FastApiProject\fastApiProject

 ╭─ Python module file ─╮
 │                      │
 │  🐍 main.py          │
 │                      │
 ╰──────────────────────╯

INFO     Importing module main
INFO     Found importable FastAPI app

 ╭─ Importable FastAPI app ─╮
 │                          │
 │  from main import app    │
 │                          │
 ╰──────────────────────────╯

INFO     Using import string main:app

 ╭────────── FastAPI CLI - Development mode ───────────╮
 │                                                     │
 │  Serving at: http://127.0.0.1:8000                  │
 │                                                     │
 │  API docs: http://127.0.0.1:8000/docs               │
 │                                                     │
 │  Running in development mode, for production use:   │
 │                                                     │
 │  fastapi run                                        │
 │                                                     │
 ╰─────────────────────────────────────────────────────╯

INFO:     Will watch for changes in these directories: ['E:\\PyCharmProject\\FastApiProject\\fastApiProject']
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [2824] using WatchFiles
INFO:     Started server process [11344]
INFO:     Waiting for application startup.
INFO:     Application startup complete.

该命令行程序 fastapi 就是 FastAPI CLI。

FastAPI CLI 接收你的 Python 程序路径,自动检测包含 FastAPI 的变量(通常命名为 app)及其导入方式,然后启动服务。

默认情况下,它将监听 IP 地址 127.0.0.1,这是你的机器与自身通信的 IP 地址(localhost)。


2.fastapi run

当你运行 fastapi run 时,它默认以生产环境模式运行。

(fastapi_env) PS E:> fastapi run
INFO     Using path main.py
INFO     Resolved absolute path E:\PyCharmProject\FastApiProject\fastApiProject\main.py
INFO     Searching for package file structure from directories with __init__.py files
INFO     Importing from E:\PyCharmProject\FastApiProject\fastApiProject

 ╭─ Python module file ─╮
 │                      │
 │  🐍 main.py          │
 │                      │
 ╰──────────────────────╯

INFO     Importing module main
INFO     Found importable FastAPI app

 ╭─ Importable FastAPI app ─╮
 │                          │
 │  from main import app    │
 │                          │
 ╰──────────────────────────╯

INFO     Using import string main:app

 ╭─────────── FastAPI CLI - Production mode ───────────╮
 │                                                     │
 │  Serving at: http://0.0.0.0:8000                    │
 │                                                     │
 │  API docs: http://0.0.0.0:8000/docs                 │
 │                                                     │
 │  Running in production mode, for development use:   │
 │                                                     │
 │  fastapi dev                                        │
 │                                                     │
 ╰─────────────────────────────────────────────────────╯

INFO:     Started server process [11000]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)

默认情况下,自动重载是禁用的。

它将监听 IP 地址 0.0.0.0,即所有可用的 IP 地址,这样任何能够与该机器通信的人都可以公开访问它。这通常是你在生产环境中运行它的方式,例如在容器中运行。

FastAPI CLI官方文档https://fastapi.tiangolo.com/zh/fastapi-cli/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

北极的三哈

你的鼓励是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值