efr 0.3__tar.gz → 0.3.2__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.
Files changed (49) hide show
  1. {efr-0.3 → efr-0.3.2}/PKG-INFO +1 -1
  2. efr-0.3.2/efr/README.md +370 -0
  3. efr-0.3.2/efr/__init__.py +56 -0
  4. {efr-0.3 → efr-0.3.2}/efr/core/__init__.py +1 -0
  5. {efr-0.3 → efr-0.3.2}/efr/core/ealloter.py +11 -3
  6. {efr-0.3 → efr-0.3.2}/efr/core/eframework.py +163 -28
  7. {efr-0.3 → efr-0.3.2}/efr/core/estation.py +26 -10
  8. efr-0.3.2/efr/demo.py +30 -0
  9. efr-0.3.2/efr/doc/examples/example_embed_usage.py +322 -0
  10. efr-0.3.2/efr/efr.md +370 -0
  11. efr-0.3.2/efr/embed/__init__.py +50 -0
  12. efr-0.3.2/efr/embed/api.py +835 -0
  13. efr-0.3.2/efr/embed/efr-api.py +835 -0
  14. efr-0.3.2/efr/test/test_auto_worker.py +424 -0
  15. efr-0.3/efr/test/test_simple_api.py → efr-0.3.2/efr/test/test_embed.py +205 -6
  16. {efr-0.3 → efr-0.3.2}/efr/test/test_framework.py +94 -9
  17. efr-0.3.2/efr/test/test_readme_examples.py +242 -0
  18. {efr-0.3 → efr-0.3.2}/efr/test/test_station.py +78 -0
  19. {efr-0.3 → efr-0.3.2}/efr/test/test_worker.py +87 -4
  20. {efr-0.3 → efr-0.3.2}/efr/utils/worker.py +42 -15
  21. {efr-0.3 → efr-0.3.2}/efr.egg-info/PKG-INFO +1 -1
  22. {efr-0.3 → efr-0.3.2}/efr.egg-info/SOURCES.txt +10 -4
  23. {efr-0.3 → efr-0.3.2}/setup.py +5 -1
  24. efr-0.3/efr/__init__.py +0 -90
  25. efr-0.3/efr/doc/examples/simple_api_demo.py +0 -163
  26. efr-0.3/efr/simple/__init__.py +0 -29
  27. efr-0.3/efr/simple/api.py +0 -543
  28. {efr-0.3 → efr-0.3.2}/efr/core/eerrors.py +0 -0
  29. {efr-0.3 → efr-0.3.2}/efr/core/equeue.py +0 -0
  30. {efr-0.3 → efr-0.3.2}/efr/core/event.py +0 -0
  31. {efr-0.3 → efr-0.3.2}/efr/doc/__init__.py +0 -0
  32. {efr-0.3 → efr-0.3.2}/efr/doc/examples/__init__.py +0 -0
  33. {efr-0.3 → efr-0.3.2}/efr/doc/examples/advanced_usage.py +0 -0
  34. {efr-0.3 → efr-0.3.2}/efr/doc/examples/basic_usage.py +0 -0
  35. {efr-0.3 → efr-0.3.2}/efr/test/__init__.py +0 -0
  36. {efr-0.3 → efr-0.3.2}/efr/test/test_event.py +0 -0
  37. {efr-0.3 → efr-0.3.2}/efr/test/test_integration.py +0 -0
  38. {efr-0.3 → efr-0.3.2}/efr/test/test_queue.py +0 -0
  39. {efr-0.3 → efr-0.3.2}/efr/test/test_task.py +0 -0
  40. {efr-0.3 → efr-0.3.2}/efr/utils/__init__.py +0 -0
  41. {efr-0.3 → efr-0.3.2}/efr/utils/exceptions.py +0 -0
  42. {efr-0.3 → efr-0.3.2}/efr/utils/functions.py +0 -0
  43. {efr-0.3 → efr-0.3.2}/efr/utils/id_generator.py +0 -0
  44. {efr-0.3 → efr-0.3.2}/efr/utils/task.py +0 -0
  45. {efr-0.3 → efr-0.3.2}/efr/utils/trace.py +0 -0
  46. {efr-0.3 → efr-0.3.2}/efr.egg-info/dependency_links.txt +0 -0
  47. {efr-0.3 → efr-0.3.2}/efr.egg-info/requires.txt +0 -0
  48. {efr-0.3 → efr-0.3.2}/efr.egg-info/top_level.txt +0 -0
  49. {efr-0.3 → efr-0.3.2}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: efr
3
- Version: 0.3
3
+ Version: 0.3.2
4
4
  Summary: ...
5
5
  Author-email: 2229066748@qq.com
6
6
  Maintainer: Eagle'sBaby
@@ -0,0 +1,370 @@
1
+ # efr - Event Framework
2
+
3
+ Python事件驱动框架,用于构建可扩展的异步应用。
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ pip install efr
9
+ ```
10
+
11
+ ## 核心概念
12
+
13
+ - **Event**: 事件单元,包含任务数据、状态、优先级等
14
+ - **EventQueue**: 线程安全的事件队列
15
+ - **EventStation**: 事件处理站点,负责过滤和处理特定事件
16
+ - **EventFramework**: 主框架,整合队列、站点和工作线程
17
+ - **EventSystem**: 嵌入式事件系统,支持独立模式或框架集成
18
+ - **Cost**: 站点/任务负载强度,用于自动worker负载均衡(默认=1)
19
+
20
+ ## 快速开始
21
+
22
+ ### 方式1: 使用 EventSystem(推荐,简单事件)
23
+
24
+ ```python
25
+ from efr import EventSystem
26
+
27
+ events = EventSystem()
28
+
29
+ # 消费者:监听任务
30
+ @events.listen("task_queue")
31
+ def consumer(data):
32
+ task_id = data["task_id"]
33
+ print(f"[Consumer] Processing task {task_id}")
34
+ return {"result": "success", "task_id": task_id}
35
+
36
+ # 生产者:推送任务
37
+ for i in range(5):
38
+ events.pushEvent("task_queue", {"task_id": i, "payload": f"data_{i}"})
39
+
40
+ events.stop()
41
+ ```
42
+
43
+ ### 方式2: 使用 EventFramework(多线程)
44
+
45
+ ```python
46
+ from efr import EventFramework, Event, EventStation
47
+ import time
48
+
49
+ # 创建框架(auto_task_count=3 表示每个自动worker最多承载3个单位的负载)
50
+ efr = EventFramework(name="producer_consumer", auto_task_count=3)
51
+
52
+ # 消费者:定义处理函数
53
+ def consumer(event):
54
+ task = event.task
55
+ print(f"[Consumer] Processing task {task['id']}")
56
+ return {"status": "done", "id": task["id"]}
57
+
58
+ # 创建消费者站点并自动分配工作线程
59
+ station = EventStation(key="task_queue", respond_fn=consumer)
60
+ efr.login(station, True) # True = 自动创建worker
61
+
62
+ # 启动框架
63
+ efr.start()
64
+
65
+ # 生产者:推送任务
66
+ for i in range(5):
67
+ event = Event(task={"id": i}, dest="task_queue")
68
+ efr.push(event)
69
+ time.sleep(0.1)
70
+
71
+ time.sleep(1) # 等待处理完成
72
+ efr.quit()
73
+ ```
74
+
75
+ ### 方式3: Embed消费 + Estation生产(组合使用)
76
+
77
+ ```python
78
+ from efr import EventFramework, EventSystem, Event
79
+ import time
80
+
81
+ # 创建框架
82
+ efr = EventFramework(name="hybrid")
83
+
84
+ # 消费者:通过EventSystem监听(auto_worker=True 自动分配工作线程)
85
+ events = EventSystem(eframework=efr)
86
+
87
+ @events.listen("job_queue")
88
+ def consumer(data):
89
+ job_id = data["job_id"]
90
+ print(f"[Consumer] Processing job {job_id}")
91
+ return {"job_id": job_id, "status": "completed"}
92
+
93
+ # 生产者:直接通过Event推送事件
94
+ for i in range(5):
95
+ event = Event(
96
+ task={"job_id": i, "data": f"payload_{i}"},
97
+ dest="job_queue",
98
+ priority=5
99
+ )
100
+ efr.push(event)
101
+ time.sleep(0.1)
102
+
103
+ # 启动并运行
104
+ efr.start()
105
+ time.sleep(1) # 等待处理完成
106
+ events.stop()
107
+ efr.quit()
108
+ ```
109
+
110
+ ## 事件状态
111
+
112
+ - `OFFLINE`: 已创建,未入队
113
+ - `JUNIOR`: 在队列中等待
114
+ - `URGENT`: 正在被处理
115
+ - `FINISH`: 已成功处理
116
+ - `RETIRED`: 已被所有站点处理
117
+ - `EXCEPT`: 处理异常
118
+
119
+ ## 高级功能
120
+
121
+ ### 带优先级的事件系统
122
+
123
+ ```python
124
+ from efr import PrioritizedEventSystem
125
+
126
+ events = PrioritizedEventSystem()
127
+
128
+ # 高优先级消费者先处理
129
+ @events.listen("task_queue", priority=10)
130
+ def urgent_consumer(data):
131
+ print(f"[Urgent] Processing high priority task {data['task_id']}")
132
+
133
+ # 低优先级消费者后处理
134
+ @events.listen("task_queue", priority=1)
135
+ def normal_consumer(data):
136
+ print(f"[Normal] Processing normal task {data['task_id']}")
137
+
138
+ # 生产者推送任务
139
+ events.pushEvent("task_queue", {"task_id": 1})
140
+ ```
141
+
142
+ ### 源过滤
143
+
144
+ ```python
145
+ # 只接收来自 "payment_service" 源的任务
146
+ @events.listen("task_queue", "payment_service")
147
+ def payment_consumer(data):
148
+ print(f"[Payment] Processing: {data}")
149
+
150
+ # 推送时指定源
151
+ events.pushEvent("task_queue", {
152
+ "source": "payment_service",
153
+ "amount": 100
154
+ })
155
+ ```
156
+
157
+ ### 动态框架切换
158
+
159
+ ```python
160
+ from efr import EventFramework, EventSystem
161
+
162
+ # 独立模式(单线程)
163
+ events = EventSystem()
164
+
165
+ # 后期附加到框架(转为多线程)
166
+ efr = EventFramework()
167
+ events.eframework = efr
168
+ efr.start()
169
+
170
+ # 现在推送的事件会走框架队列
171
+ events.pushEvent("task_queue", {"task_id": 1})
172
+
173
+ # 分离回独立模式
174
+ del events.eframework
175
+ ```
176
+
177
+ ## API参考
178
+
179
+ ### EventFramework
180
+
181
+ | 方法/属性 | 说明 |
182
+ |-----------|----------------------------------------------------------------------------|
183
+ | `__init__(..., auto_task_count=1)` | 初始化,`auto_task_count`设置每个自动worker的负载(cost)上限[1,10] |
184
+ | `start()` | 启动框架和工作线程 |
185
+ | `quit()` | 停止框架 |
186
+ | `push(event)` | 推送事件到队列 |
187
+ | `login(station, worker=True)` | 注册站点,`worker`可为`True`(自动,使用station.cost)、`int`(使用指定cost)、`Worker`实例或`None` |
188
+ | `logoff(station)` | 注销站点 |
189
+ | `add_worker(name)` | 添加工作线程(手动创建,不会被自动释放) |
190
+ | `assign(station, worker=True)` | 分配worker,`worker`可为`True`(自动)、`int`(指定cost)、`Worker`实例或`None`(取消分配) |
191
+ | `auto_task_count` | 获取/设置自动worker的cost上限,修改后同步到所有自动worker |
192
+
193
+ ### EventSystem
194
+
195
+ | 方法 | 说明 |
196
+ |------|------|
197
+ | `__init__(eframework=None, auto_worker=True)` | 初始化,`auto_worker`控制是否自动分配worker及是否使用cost机制 |
198
+ | `listenFor(event, callback, *sources)` | 注册监听器 |
199
+ | `listen(event, *sources)` | 装饰器方式注册 |
200
+ | `pushEvent(event, data, *targets)` | 推送事件 |
201
+ | `cancelListen(event, callback)` | 取消监听 |
202
+ | `hasListeners(event)` | 检查是否有监听器 |
203
+ | `clear()` | 清除所有监听器 |
204
+ | `stop()` | 停止并清理 |
205
+
206
+ **属性**
207
+
208
+ | 属性 | 说明 |
209
+ |------|------|
210
+ | `eframework` | 获取/设置关联的框架。设为None等价于删除。切换时会自动清理旧框架的worker绑定 |
211
+
212
+ ### Worker 负载管理(Cost 机制)
213
+
214
+ 框架通过 **cost** 机制实现基于负载强度的自动工作线程分配。
215
+
216
+ **基本概念**
217
+
218
+ - **Station Cost**: 每个 EventStation 的负载强度(默认=1)
219
+ - **Task Cost**: 分配给 Worker 的每个任务的负载值(默认=1)
220
+ - **Worker Capacity**: `auto_task_count` 定义 Worker 的负载上限(1-10,默认=1)
221
+
222
+ **分配规则**
223
+
224
+ 当自动分配 Worker 时(`login(station, True)` 或 `login(station, int)`):
225
+ 1. 检查现有自动 Worker 的 `total_cost()` 是否 `< auto_task_count`
226
+ 2. 如果有容量,分配给该 Worker
227
+ 3. 如果无容量,创建新的自动 Worker
228
+
229
+ ```python
230
+ # 创建框架(每个自动worker的cost上限为5)
231
+ efr = EventFramework(name="app", auto_task_count=5)
232
+
233
+ # 创建站点并指定cost(负载强度)
234
+ # 高负载站点(cost=3)
235
+ heavy_station = EventStation(key="heavy", respond_fn=process_heavy, cost=3)
236
+ efr.login(heavy_station, True) # 占用3/5的capacity
237
+
238
+ # 低负载站点(cost=1,默认)
239
+ light_station = EventStation(key="light", respond_fn=process_light, cost=1)
240
+ efr.login(light_station, True) # 可与heavy_station共用同一个worker(3+1=4 < 5)
241
+
242
+ # 使用自定义int覆盖station的cost
243
+ # 这将使用cost=4而不是station.cost=3
244
+ efr.login(heavy_station, worker=4) # 强制使用cost=4
245
+ ```
246
+
247
+ **类级默认 Cost**
248
+
249
+ 通过类属性 `COST` 设置子类的默认 cost:
250
+
251
+ ```python
252
+ # 定义高负载站点类型
253
+ class HeavyStation(EventStation):
254
+ COST = 5 # 类级默认cost
255
+
256
+ # 自动使用类级cost
257
+ station = HeavyStation(key="heavy") # cost = 5
258
+
259
+ # 实例级覆盖
260
+ station2 = HeavyStation(key="medium", cost=3) # cost = 3
261
+
262
+ # 运行时修改类级默认值
263
+ EventStation.COST = 2 # 影响所有新创建的实例
264
+ ```
265
+
266
+ **手动分配 Worker**
267
+
268
+ 手动创建的 Worker 不受 cost 限制,也不会被自动释放:
269
+
270
+ ```python
271
+ # 手动创建worker(不会被自动释放)
272
+ manual_worker = efr.add_worker("manual")
273
+ efr.login(station, manual_worker) # 不使用cost机制
274
+
275
+ # 仅注册站点,不分配worker
276
+ efr.login(station, None)
277
+
278
+ # 取消worker分配,释放auto worker
279
+ efr.assign(station, None)
280
+ ```
281
+
282
+ **动态调整负载上限**
283
+
284
+ 修改 `auto_task_count` 会同步到所有自动管理的 Worker:
285
+
286
+ ```python
287
+ # 初始上限为3
288
+ efr = EventFramework(auto_task_count=3)
289
+ efr.login(station1, True) # 分配给worker_0
290
+
291
+ # 调整为5,所有自动worker同步更新
292
+ efr.auto_task_count = 5 # worker_0.auto_task_count 也变为5
293
+ ```
294
+
295
+ **API 行为总结**
296
+
297
+ | 方法 | 行为 |
298
+ |------|------|
299
+ | `login(station, True)` | 自动分配,使用 `station.cost` |
300
+ | `login(station, int)` | 自动分配,使用指定的 int 作为 cost(覆盖 station.cost) |
301
+ | `login(station, worker)` | 使用指定 Worker,忽略 cost 机制 |
302
+ | `login(station, None)` | 仅注册站点,不分配 Worker |
303
+ | `assign(station, True)` | 同 `login(station, True)` |
304
+ | `assign(station, int)` | 同 `login(station, int)` |
305
+ | `assign(station, None)` | 取消分配,释放 auto worker |
306
+
307
+ ### EventStation
308
+
309
+ | 方法/属性 | 说明 |
310
+ |-----------|------|
311
+ | `__init__(..., cost=None)` | 初始化站点,`cost=None`时使用类属性`COST` |
312
+ | `cost` | 获取/设置站点的负载强度(最小值为1) |
313
+ | `COST` (类属性) | 类级默认cost,所有实例默认使用此值 |
314
+ | `key` | 站点标识符 |
315
+ | `level` | 优先级(越高越优先处理) |
316
+ | `filter(event)` | 过滤事件 |
317
+ | `respond(event)` | 处理事件 |
318
+
319
+ ```python
320
+ # 使用类级默认cost
321
+ class HeavyStation(EventStation):
322
+ COST = 5
323
+
324
+ station = HeavyStation(key="heavy") # cost = 5
325
+ ```
326
+
327
+ ### Worker
328
+
329
+ | 方法/属性 | 说明 |
330
+ |-----------|------|
331
+ | `__init__(..., auto_managed=False, auto_task_count=1)` | 初始化worker |
332
+ | `add_task(task, cost=1)` | 添加任务,`cost`仅在auto_managed模式下追踪 |
333
+ | `total_cost()` | 获取所有任务的cost总和(仅auto_managed模式) |
334
+ | `auto_task_count` | 获取/设置cost上限[1,10] |
335
+ | `auto_managed` | 是否为自动管理(无任务时自动释放) |
336
+
337
+ ### PrioritizedEventSystem
338
+
339
+ 与 EventSystem 相同,额外支持 `priority` 参数:
340
+
341
+ ```python
342
+ @events.listen("event", priority=10) # priority越高越早处理
343
+ def handler(data):
344
+ pass
345
+ ```
346
+
347
+ ## 项目结构
348
+
349
+ ```
350
+ efr/
351
+ ├── core/ # 核心组件
352
+ │ ├── event.py # Event类
353
+ │ ├── equeue.py # 事件队列
354
+ │ ├── estation.py # 处理站点
355
+ │ ├── ealloter.py # 事件分配器
356
+ │ ├── eframework.py # 主框架
357
+ │ └── eerrors.py # 异常类
358
+ ├── embed/ # 嵌入式事件系统
359
+ │ └── api.py # EventSystem
360
+ ├── utils/ # 工具类
361
+ │ ├── worker.py # 工作线程
362
+ │ └── task.py # 任务单元
363
+ └── doc/examples/ # 示例代码
364
+ ```
365
+
366
+ ## 依赖
367
+
368
+ - Python >= 3.10
369
+ - loguru >= 0.7.0
370
+
@@ -0,0 +1,56 @@
1
+ """
2
+ efr - Event Framework Library
3
+
4
+ A Python event-driven framework for building scalable applications.
5
+
6
+ Main Components:
7
+ EventSystem: Simple event system (recommended for most use cases)
8
+ PrioritizedEventSystem: Event system with priority support
9
+ EventFramework: Multi-threaded event framework
10
+ Event, EventStation, EventQueue: Core event components
11
+
12
+ Example:
13
+ >>> from efr import EventSystem
14
+ >>>
15
+ >>> events = EventSystem()
16
+ >>> events.listenFor("data", process_data)
17
+ >>> events.pushEvent("data", {"key": "value"})
18
+ """
19
+
20
+ __version__ = "0.1.0"
21
+
22
+ # Import core components for convenience
23
+ from efr.core.event import Event, EventState
24
+ from efr.core.equeue import EventQueue
25
+ from efr.core.estation import EventStation
26
+ from efr.core.ealloter import EventAlloter
27
+ from efr.core.eframework import EventFramework
28
+ from efr.core.eerrors import (
29
+ SolutionMissing,
30
+ WorkerError,
31
+ TaskError,
32
+ FrameworkError
33
+ )
34
+
35
+ # Import embed components for convenience
36
+ from efr.embed.api import EventSystem, PrioritizedEventSystem
37
+
38
+ __all__ = [
39
+ # Version
40
+ "__version__",
41
+ # Core components
42
+ "Event",
43
+ "EventState",
44
+ "EventQueue",
45
+ "EventStation",
46
+ "EventAlloter",
47
+ "EventFramework",
48
+ # Embed components
49
+ "EventSystem",
50
+ "PrioritizedEventSystem",
51
+ # Errors
52
+ "SolutionMissing",
53
+ "WorkerError",
54
+ "TaskError",
55
+ "FrameworkError",
56
+ ]
@@ -7,6 +7,7 @@ This module provides the fundamental components of the event-driven framework:
7
7
  - EventStation: Event processing station
8
8
  - EventAlloter: Event allocator for distributing events
9
9
  - EventFramework: Main framework integrating all components
10
+ - eerrors: Error classes for the framework
10
11
  """
11
12
 
12
13
  from efr.core.event import Event, EventState
@@ -73,15 +73,23 @@ class EventAlloter:
73
73
  def login(self, station: EventStation) -> bool:
74
74
  """
75
75
  Register a station with the alloter.
76
-
76
+
77
77
  Stations are sorted by level (higher = earlier in list).
78
-
78
+
79
79
  Args:
80
80
  station: The station to register
81
-
81
+
82
82
  Returns:
83
83
  True if registered successfully, False if already registered
84
+
85
+ Raises:
86
+ TypeError: If station is not an EventStation instance
84
87
  """
88
+ if not isinstance(station, EventStation):
89
+ raise TypeError(
90
+ f"Expected station to be an instance of EventStation, "
91
+ f"but got {type(station).__name__}"
92
+ )
85
93
  if station in self._stations:
86
94
  return False
87
95