Skip to content

教程 - 用户指南

Warning

The current page still doesn't have a translation for this language. But you can help translating it: Contributing.

本教程将一步步向你展示如何使用 FastAPI-Channels 的绝大部分特性。

各个章节的内容循序渐进,但是又围绕着单独的主题,所以你可以直接跳转到某个章节以解决你的特定需求。

本教程同样可以作为将来的参考手册。

你可以随时回到本教程并查阅你需要的内容。

运行代码

所有代码片段都可以复制后直接使用(它们实际上是经过测试的 Python 文件)。

要运行任何示例,请将代码复制到 main.py 文件中,然后使用以下命令启动 fastapi

fast →fastapi dev main.pyINFO Using path main.py
INFO Resolved absolute path /home/user/code/awesomeapp/main.py
INFO Searching for package file structure from directories with __init__.py files
INFO Importing from /home/user/code/awesomeapp

╭─ 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: ['/home/user/code/awesomeapp']
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [2265862] using WatchFiles
INFO: Started server process [2265873]
INFO: Waiting for application startup.
INFO: Application startup complete.


restart ↻

安装 FastAPI-Channels

第一个步骤是安装 FastAPI-Channels和它所需要的基础依赖项。 FastAPI了是必不可少的,而Broadcaster和FastAPI-limiter直接安装FastAPI-Channels就可以了

确保你创建了一个 虚拟环境,激活它,然后进行下面的安装步骤:

fast →pip install fastapi fastapi-channels

restart ↻

你还可以通过指定安装FastAPI-Channels额外的依赖项,来满足你当前使用的后端需求,如:redis、postgres、kafka

fast →pip install "fastapi-channels[postgres]"

restart ↻

高级用户指南

在本 教程-用户指南 之后,你可以阅读 高级用户指南

高级用户指南 以本教程为基础,使用相同的概念,并教授一些额外的特性。

但是你应该先阅读 教程-用户指南(即你现在正在阅读的内容)。

教程经过精心设计,使你可以仅通过 教程-用户指南 来开发一个完整的应用程序,然后根据你的需要,使用 高级用户指南 中的一些其他概念,以不同的方式来扩展它。

Was this page helpful?

Comments