infinity_bus 1.3.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,9 @@
1
+ # The MIT License (MIT)
2
+
3
+ Copyright © 2026 YinBailiang
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,160 @@
1
+ Metadata-Version: 2.4
2
+ Name: infinity_bus
3
+ Version: 1.3.1
4
+ Summary: 基于 asyncio 的轻量级事件总线,支持发布/订阅、正则匹配、背压控制与优雅停机
5
+ Author: EventBus Authors
6
+ License: MIT
7
+ Project-URL: Repository, https://github.com/yinbailiang/event_bus
8
+ Keywords: event-bus,async,asyncio,pub-sub,publish-subscribe,event-driven
9
+ Classifier: Development Status :: 4 - Beta
10
+ Classifier: Intended Audience :: Developers
11
+ Classifier: License :: OSI Approved :: MIT License
12
+ Classifier: Operating System :: OS Independent
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.12
15
+ Classifier: Programming Language :: Python :: 3.13
16
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
17
+ Classifier: Framework :: AsyncIO
18
+ Requires-Python: >=3.12
19
+ Description-Content-Type: text/markdown
20
+ License-File: LICENSE.md
21
+ Requires-Dist: aiosqlite
22
+ Requires-Dist: pydantic
23
+ Provides-Extra: test
24
+ Requires-Dist: pytest; extra == "test"
25
+ Requires-Dist: pytest-cov; extra == "test"
26
+ Requires-Dist: pytest-asyncio; extra == "test"
27
+ Provides-Extra: dev
28
+ Requires-Dist: pytest; extra == "dev"
29
+ Requires-Dist: pytest-cov; extra == "dev"
30
+ Requires-Dist: pytest-asyncio; extra == "dev"
31
+ Dynamic: license-file
32
+
33
+ # InfinityBus — 异步事件总线
34
+
35
+ [![Test](https://github.com/yinbailiang/event_bus/actions/workflows/test.yml/badge.svg)](https://github.com/yinbailiang/event_bus/actions/workflows/test.yml)
36
+ [![Python](https://img.shields.io/badge/python-3.12%2B-blue)](https://www.python.org/)
37
+ [![License](https://img.shields.io/badge/license-MIT-green)](LICENSE.md)
38
+
39
+ 基于 **asyncio** 的轻量级事件总线,实现发布/订阅模式,用于在异步应用中解耦组件间的通信。
40
+
41
+ ## ✨ 特性
42
+
43
+ - **强类型负载校验** — 基于 Pydantic,发布时自动校验数据类型与结构
44
+ - **正则表达式订阅** — 支持灵活的事件名匹配规则
45
+ - **背压控制** — 队列大小与并发信号量双重限流,防止过载
46
+ - **超时保护** — 每个处理器可独立设置超时,避免单任务阻塞总线
47
+ - **错误隔离** — 单个处理器异常不影响其他处理器,错误通过内置事件统一上报
48
+ - **优雅停机** — 保证停止过程中已入队事件被完整处理,避免数据丢失
49
+ - **可观测性** — 提供活跃任务数、队列长度等监控指标
50
+
51
+ ## 📦 安装
52
+
53
+ ```bash
54
+ pip install git+https://github.com/yinbailiang/infinity_bus.git
55
+ ```
56
+
57
+ 或从源码安装:
58
+
59
+ ```bash
60
+ git clone https://github.com/yinbailiang/infinity_bus.git
61
+ cd event_bus
62
+ pip install -e ".[test]"
63
+ ```
64
+
65
+ ## 🚀 快速开始
66
+
67
+ ### 基础发布/订阅
68
+
69
+ ```python
70
+ import asyncio
71
+ from pydantic import BaseModel
72
+ from event_bus import (
73
+ EventBus, EventDeclaration, EventHandler,
74
+ EventRegistry, EventHandlerRegistry,
75
+ )
76
+
77
+ # 1. 定义负载
78
+ class MyPayload(BaseModel):
79
+ message: str
80
+
81
+ # 2. 声明事件
82
+ class MyEvent(EventDeclaration):
83
+ name = "my.event"
84
+ payload_type = MyPayload
85
+
86
+ # 3. 实现处理器
87
+ class MyHandler(EventHandler):
88
+ def __init__(self):
89
+ super().__init__(subscriptions=["my.event"])
90
+
91
+ async def handle(self, payload, bus_proxy, raw_event):
92
+ print(f"Received: {payload.message}")
93
+
94
+ # 4. 组装并运行
95
+ async def main():
96
+ reg = EventRegistry()
97
+ reg.register(MyEvent)
98
+ h_reg = EventHandlerRegistry()
99
+ h_reg.register(MyHandler())
100
+
101
+ async with EventBus(reg, h_reg) as bus:
102
+ await bus.proxy("cli").publish("my.event", {"message": "Hello, EventBus!"})
103
+ await asyncio.sleep(1) # 等待处理器输出
104
+
105
+ asyncio.run(main())
106
+ ```
107
+
108
+ ### 请求-响应模式
109
+
110
+ ```python
111
+ from event_bus.templates.request import request, RequestProtocol, ResponseProtocol
112
+
113
+ # 定义请求/响应负载与事件(详见文档)
114
+ # ...
115
+
116
+ resp = await request(
117
+ bus_proxy=proxy,
118
+ req_event="user.get.request",
119
+ req_data={"user_id": 123},
120
+ resp_event="user.get.response",
121
+ timeout=10.0,
122
+ )
123
+ resp.raise_if_failed()
124
+ ```
125
+
126
+ ## 🧱 架构
127
+
128
+ | 组件 | 职责 |
129
+ | - | - |
130
+ | **Event** | 运行时事件实例,含名称、负载、处理链追踪 |
131
+ | **EventDeclaration** | 事件类型元数据声明(名称 + 可选 Pydantic 负载模型) |
132
+ | **EventRegistry** | 集中管理已注册的事件声明,发布时校验 |
133
+ | **EventHandler** | 处理器基类,实现 `handle` 方法定义业务逻辑 |
134
+ | **EventHandlerRegistry** | 管理处理器实例,按事件名匹配处理器列表 |
135
+ | **EventBus** | 事件分发中枢:任务队列、并发控制、错误上报、生命周期 |
136
+
137
+ ## 📚 文档
138
+
139
+ - [EventBus 核心](docs/event_bus.md)
140
+ - [Expect 一次性监听器](docs/templates/expect.md)
141
+ - [Request 请求-响应模板](docs/templates/request.md)
142
+ - [Pipe 双向管道](docs/templates/pipe.md)
143
+ - [Register 模块事件注册器](docs/templates/register.md)
144
+
145
+ ## 🧪 测试
146
+
147
+ ```bash
148
+ # 运行全部测试
149
+ pytest --cov=src -v
150
+
151
+ # 仅运行核心测试
152
+ pytest tests/event_bus_test.py -v
153
+
154
+ # 仅运行模板测试
155
+ pytest tests/templates/ -v
156
+ ```
157
+
158
+ ## 📄 许可证
159
+
160
+ [MIT](LICENSE.md)
@@ -0,0 +1,128 @@
1
+ # InfinityBus — 异步事件总线
2
+
3
+ [![Test](https://github.com/yinbailiang/event_bus/actions/workflows/test.yml/badge.svg)](https://github.com/yinbailiang/event_bus/actions/workflows/test.yml)
4
+ [![Python](https://img.shields.io/badge/python-3.12%2B-blue)](https://www.python.org/)
5
+ [![License](https://img.shields.io/badge/license-MIT-green)](LICENSE.md)
6
+
7
+ 基于 **asyncio** 的轻量级事件总线,实现发布/订阅模式,用于在异步应用中解耦组件间的通信。
8
+
9
+ ## ✨ 特性
10
+
11
+ - **强类型负载校验** — 基于 Pydantic,发布时自动校验数据类型与结构
12
+ - **正则表达式订阅** — 支持灵活的事件名匹配规则
13
+ - **背压控制** — 队列大小与并发信号量双重限流,防止过载
14
+ - **超时保护** — 每个处理器可独立设置超时,避免单任务阻塞总线
15
+ - **错误隔离** — 单个处理器异常不影响其他处理器,错误通过内置事件统一上报
16
+ - **优雅停机** — 保证停止过程中已入队事件被完整处理,避免数据丢失
17
+ - **可观测性** — 提供活跃任务数、队列长度等监控指标
18
+
19
+ ## 📦 安装
20
+
21
+ ```bash
22
+ pip install git+https://github.com/yinbailiang/infinity_bus.git
23
+ ```
24
+
25
+ 或从源码安装:
26
+
27
+ ```bash
28
+ git clone https://github.com/yinbailiang/infinity_bus.git
29
+ cd event_bus
30
+ pip install -e ".[test]"
31
+ ```
32
+
33
+ ## 🚀 快速开始
34
+
35
+ ### 基础发布/订阅
36
+
37
+ ```python
38
+ import asyncio
39
+ from pydantic import BaseModel
40
+ from event_bus import (
41
+ EventBus, EventDeclaration, EventHandler,
42
+ EventRegistry, EventHandlerRegistry,
43
+ )
44
+
45
+ # 1. 定义负载
46
+ class MyPayload(BaseModel):
47
+ message: str
48
+
49
+ # 2. 声明事件
50
+ class MyEvent(EventDeclaration):
51
+ name = "my.event"
52
+ payload_type = MyPayload
53
+
54
+ # 3. 实现处理器
55
+ class MyHandler(EventHandler):
56
+ def __init__(self):
57
+ super().__init__(subscriptions=["my.event"])
58
+
59
+ async def handle(self, payload, bus_proxy, raw_event):
60
+ print(f"Received: {payload.message}")
61
+
62
+ # 4. 组装并运行
63
+ async def main():
64
+ reg = EventRegistry()
65
+ reg.register(MyEvent)
66
+ h_reg = EventHandlerRegistry()
67
+ h_reg.register(MyHandler())
68
+
69
+ async with EventBus(reg, h_reg) as bus:
70
+ await bus.proxy("cli").publish("my.event", {"message": "Hello, EventBus!"})
71
+ await asyncio.sleep(1) # 等待处理器输出
72
+
73
+ asyncio.run(main())
74
+ ```
75
+
76
+ ### 请求-响应模式
77
+
78
+ ```python
79
+ from event_bus.templates.request import request, RequestProtocol, ResponseProtocol
80
+
81
+ # 定义请求/响应负载与事件(详见文档)
82
+ # ...
83
+
84
+ resp = await request(
85
+ bus_proxy=proxy,
86
+ req_event="user.get.request",
87
+ req_data={"user_id": 123},
88
+ resp_event="user.get.response",
89
+ timeout=10.0,
90
+ )
91
+ resp.raise_if_failed()
92
+ ```
93
+
94
+ ## 🧱 架构
95
+
96
+ | 组件 | 职责 |
97
+ | - | - |
98
+ | **Event** | 运行时事件实例,含名称、负载、处理链追踪 |
99
+ | **EventDeclaration** | 事件类型元数据声明(名称 + 可选 Pydantic 负载模型) |
100
+ | **EventRegistry** | 集中管理已注册的事件声明,发布时校验 |
101
+ | **EventHandler** | 处理器基类,实现 `handle` 方法定义业务逻辑 |
102
+ | **EventHandlerRegistry** | 管理处理器实例,按事件名匹配处理器列表 |
103
+ | **EventBus** | 事件分发中枢:任务队列、并发控制、错误上报、生命周期 |
104
+
105
+ ## 📚 文档
106
+
107
+ - [EventBus 核心](docs/event_bus.md)
108
+ - [Expect 一次性监听器](docs/templates/expect.md)
109
+ - [Request 请求-响应模板](docs/templates/request.md)
110
+ - [Pipe 双向管道](docs/templates/pipe.md)
111
+ - [Register 模块事件注册器](docs/templates/register.md)
112
+
113
+ ## 🧪 测试
114
+
115
+ ```bash
116
+ # 运行全部测试
117
+ pytest --cov=src -v
118
+
119
+ # 仅运行核心测试
120
+ pytest tests/event_bus_test.py -v
121
+
122
+ # 仅运行模板测试
123
+ pytest tests/templates/ -v
124
+ ```
125
+
126
+ ## 📄 许可证
127
+
128
+ [MIT](LICENSE.md)
@@ -0,0 +1,69 @@
1
+ [project]
2
+ name = "infinity_bus"
3
+ version = "1.3.1"
4
+ description = "基于 asyncio 的轻量级事件总线,支持发布/订阅、正则匹配、背压控制与优雅停机"
5
+ readme = "README.md"
6
+ license = { text = "MIT" }
7
+ authors = [
8
+ { name = "EventBus Authors" }
9
+ ]
10
+ keywords = [
11
+ "event-bus",
12
+ "async",
13
+ "asyncio",
14
+ "pub-sub",
15
+ "publish-subscribe",
16
+ "event-driven",
17
+ ]
18
+ classifiers = [
19
+ "Development Status :: 4 - Beta",
20
+ "Intended Audience :: Developers",
21
+ "License :: OSI Approved :: MIT License",
22
+ "Operating System :: OS Independent",
23
+ "Programming Language :: Python :: 3",
24
+ "Programming Language :: Python :: 3.12",
25
+ "Programming Language :: Python :: 3.13",
26
+ "Topic :: Software Development :: Libraries :: Python Modules",
27
+ "Framework :: AsyncIO",
28
+ ]
29
+ requires-python = ">=3.12"
30
+
31
+ dependencies = [
32
+ # log
33
+ "aiosqlite",
34
+
35
+ # data
36
+ "pydantic",
37
+ ]
38
+
39
+ [project.urls]
40
+ Repository = "https://github.com/yinbailiang/event_bus"
41
+
42
+ [project.optional-dependencies]
43
+ test = [
44
+ "pytest",
45
+ "pytest-cov",
46
+ "pytest-asyncio",
47
+ ]
48
+ dev = [
49
+ "pytest",
50
+ "pytest-cov",
51
+ "pytest-asyncio",
52
+ ]
53
+
54
+ [tool.pytest.ini_options]
55
+ asyncio_mode = "auto"
56
+ addopts = "-m 'not slow'"
57
+ markers = [
58
+ "slow: long-running stress / stability tests",
59
+ ]
60
+
61
+ [build-system]
62
+ requires = ["setuptools>=61.0"]
63
+ build-backend = "setuptools.build_meta"
64
+
65
+ [tool.setuptools]
66
+ package-dir = {"" = "src"}
67
+
68
+ [tool.setuptools.packages.find]
69
+ where = ["src"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
@@ -0,0 +1,45 @@
1
+ """event_bus - 基于 asyncio 的轻量级事件总线"""
2
+
3
+ __version__ = "1.3.0"
4
+
5
+ from .event import (
6
+ Event,
7
+ EventDeclaration,
8
+ EventRegistry,
9
+ )
10
+
11
+ from .handler import (
12
+ EventHandler,
13
+ EventHandlerRegistry,
14
+ )
15
+
16
+ from .middleware import (
17
+ Middleware,
18
+ MiddlewareChain,
19
+ )
20
+
21
+ from .bus import (
22
+ EventBus,
23
+ BusShuttingDown,
24
+ ShutdownEvent,
25
+ TaskErrorPayload,
26
+ TaskErrorEvent,
27
+ ShutdownConfig,
28
+ )
29
+
30
+ __all__: list[str] = [
31
+ "__version__",
32
+ "Event",
33
+ "EventDeclaration",
34
+ "EventRegistry",
35
+ "EventHandler",
36
+ "EventHandlerRegistry",
37
+ "Middleware",
38
+ "MiddlewareChain",
39
+ "EventBus",
40
+ "BusShuttingDown",
41
+ "ShutdownEvent",
42
+ "TaskErrorPayload",
43
+ "TaskErrorEvent",
44
+ "ShutdownConfig",
45
+ ]