ASGI-Lifespan 项目教程
1. 项目的目录结构及介绍
ASGI-Lifespan 项目的目录结构如下:
asgi-lifespan/
├── src/
│ └── asgi_lifespan/
│ ├── __init__.py
│ └── manager.py
├── tests/
│ ├── __init__.py
│ └── test_lifespan.py
├── .gitignore
├── CHANGELOG.md
├── LICENSE
├── MANIFEST.in
├── Makefile
├── README.md
├── pyproject.toml
├── requirements.txt
├── setup.cfg
└── setup.py
目录结构介绍
src/asgi_lifespan/
: 包含项目的主要源代码。__init__.py
: 模块初始化文件。manager.py
: 包含 LifespanManager 类,用于处理 ASGI 应用的启动和关闭事件。
tests/
: 包含项目的测试代码。__init__.py
: 测试模块初始化文件。test_lifespan.py
: 包含 LifespanManager 的测试用例。
.gitignore
: Git 忽略文件配置。CHANGELOG.md
: 项目更新日志。LICENSE
: 项目许可证。MANIFEST.in
: 打包配置文件。Makefile
: 项目构建脚本。README.md
: 项目说明文档。pyproject.toml
: 项目配置文件。requirements.txt
: 项目依赖文件。setup.cfg
: 项目安装配置文件。setup.py
: 项目安装脚本。
2. 项目的启动文件介绍
ASGI-Lifespan 项目的启动文件主要是 manager.py
,其中定义了 LifespanManager
类,用于处理 ASGI 应用的启动和关闭事件。
manager.py
文件介绍
from contextlib import asynccontextmanager
class LifespanManager:
def __init__(self, app):
self.app = app
@asynccontextmanager
async def start(self):
# 启动逻辑
yield
# 关闭逻辑
LifespanManager
类:用于管理 ASGI 应用的启动和关闭。start
方法:使用asynccontextmanager
装饰器,提供异步上下文管理器,用于启动和关闭 ASGI 应用。
3. 项目的配置文件介绍
ASGI-Lifespan 项目的配置文件主要是 pyproject.toml
和 setup.cfg
。
pyproject.toml
文件介绍
[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"
[project]
name = "asgi-lifespan"
version = "2.1.0"
description = "Programmatically send startup/shutdown lifespan events into ASGI applications"
authors = [
{ name="Florimond Manca", email="florimond.manca@gmail.com" }
]
license = { file="LICENSE" }
readme = "README.md"
requires-python = ">=3.7"
dependencies = []
classifiers = [
"Development Status :: 5 - Production/Stable",
"Framework :: AsyncIO",
"Framework :: Trio",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
]
[build-system]
:定义构建系统的要求和后端。[project]
:定义项目的基本信息,如名称、版本、描述、作者、许可证等。
setup.cfg
文件介绍
[metadata]
name = asgi-lifespan
version = 2.1.0
description = Programmatically send startup/shutdown lifespan events into ASGI applications
long_description = file: README.md
long_description_content_type = text/markdown
author = Florimond Manca
author_email = florimond.manca@
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考