efr 0.3.2__tar.gz → 0.3.3__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 (46) hide show
  1. {efr-0.3.2 → efr-0.3.3}/PKG-INFO +1 -1
  2. {efr-0.3.2 → efr-0.3.3}/efr/README.md +1 -1
  3. {efr-0.3.2 → efr-0.3.3}/efr/test/test_auto_worker.py +3 -3
  4. {efr-0.3.2 → efr-0.3.3}/efr/test/test_integration.py +1 -2
  5. efr-0.3.3/efr/test/test_responsiveness.py +140 -0
  6. {efr-0.3.2 → efr-0.3.3}/efr/utils/worker.py +33 -14
  7. {efr-0.3.2 → efr-0.3.3}/efr.egg-info/PKG-INFO +1 -1
  8. {efr-0.3.2 → efr-0.3.3}/efr.egg-info/SOURCES.txt +1 -0
  9. {efr-0.3.2 → efr-0.3.3}/setup.py +1 -1
  10. {efr-0.3.2 → efr-0.3.3}/efr/__init__.py +0 -0
  11. {efr-0.3.2 → efr-0.3.3}/efr/core/__init__.py +0 -0
  12. {efr-0.3.2 → efr-0.3.3}/efr/core/ealloter.py +0 -0
  13. {efr-0.3.2 → efr-0.3.3}/efr/core/eerrors.py +0 -0
  14. {efr-0.3.2 → efr-0.3.3}/efr/core/eframework.py +0 -0
  15. {efr-0.3.2 → efr-0.3.3}/efr/core/equeue.py +0 -0
  16. {efr-0.3.2 → efr-0.3.3}/efr/core/estation.py +0 -0
  17. {efr-0.3.2 → efr-0.3.3}/efr/core/event.py +0 -0
  18. {efr-0.3.2 → efr-0.3.3}/efr/demo.py +0 -0
  19. {efr-0.3.2 → efr-0.3.3}/efr/doc/__init__.py +0 -0
  20. {efr-0.3.2 → efr-0.3.3}/efr/doc/examples/__init__.py +0 -0
  21. {efr-0.3.2 → efr-0.3.3}/efr/doc/examples/advanced_usage.py +0 -0
  22. {efr-0.3.2 → efr-0.3.3}/efr/doc/examples/basic_usage.py +0 -0
  23. {efr-0.3.2 → efr-0.3.3}/efr/doc/examples/example_embed_usage.py +0 -0
  24. {efr-0.3.2 → efr-0.3.3}/efr/efr.md +0 -0
  25. {efr-0.3.2 → efr-0.3.3}/efr/embed/__init__.py +0 -0
  26. {efr-0.3.2 → efr-0.3.3}/efr/embed/api.py +0 -0
  27. {efr-0.3.2 → efr-0.3.3}/efr/embed/efr-api.py +0 -0
  28. {efr-0.3.2 → efr-0.3.3}/efr/test/__init__.py +0 -0
  29. {efr-0.3.2 → efr-0.3.3}/efr/test/test_embed.py +0 -0
  30. {efr-0.3.2 → efr-0.3.3}/efr/test/test_event.py +0 -0
  31. {efr-0.3.2 → efr-0.3.3}/efr/test/test_framework.py +0 -0
  32. {efr-0.3.2 → efr-0.3.3}/efr/test/test_queue.py +0 -0
  33. {efr-0.3.2 → efr-0.3.3}/efr/test/test_readme_examples.py +0 -0
  34. {efr-0.3.2 → efr-0.3.3}/efr/test/test_station.py +0 -0
  35. {efr-0.3.2 → efr-0.3.3}/efr/test/test_task.py +0 -0
  36. {efr-0.3.2 → efr-0.3.3}/efr/test/test_worker.py +0 -0
  37. {efr-0.3.2 → efr-0.3.3}/efr/utils/__init__.py +0 -0
  38. {efr-0.3.2 → efr-0.3.3}/efr/utils/exceptions.py +0 -0
  39. {efr-0.3.2 → efr-0.3.3}/efr/utils/functions.py +0 -0
  40. {efr-0.3.2 → efr-0.3.3}/efr/utils/id_generator.py +0 -0
  41. {efr-0.3.2 → efr-0.3.3}/efr/utils/task.py +0 -0
  42. {efr-0.3.2 → efr-0.3.3}/efr/utils/trace.py +0 -0
  43. {efr-0.3.2 → efr-0.3.3}/efr.egg-info/dependency_links.txt +0 -0
  44. {efr-0.3.2 → efr-0.3.3}/efr.egg-info/requires.txt +0 -0
  45. {efr-0.3.2 → efr-0.3.3}/efr.egg-info/top_level.txt +0 -0
  46. {efr-0.3.2 → efr-0.3.3}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: efr
3
- Version: 0.3.2
3
+ Version: 0.3.3
4
4
  Summary: ...
5
5
  Author-email: 2229066748@qq.com
6
6
  Maintainer: Eagle'sBaby
@@ -181,7 +181,7 @@ del events.eframework
181
181
  | 方法/属性 | 说明 |
182
182
  |-----------|----------------------------------------------------------------------------|
183
183
  | `__init__(..., auto_task_count=1)` | 初始化,`auto_task_count`设置每个自动worker的负载(cost)上限[1,10] |
184
- | `start()` | 启动框架和工作线程 |
184
+ | `start()` | 启动框架和工作线程 |
185
185
  | `quit()` | 停止框架 |
186
186
  | `push(event)` | 推送事件到队列 |
187
187
  | `login(station, worker=True)` | 注册站点,`worker`可为`True`(自动,使用station.cost)、`int`(使用指定cost)、`Worker`实例或`None` |
@@ -79,7 +79,7 @@ def test_auto_worker_not_released_if_has_other_tasks():
79
79
  auto_worker = efr._auto_workers.get("station1")
80
80
 
81
81
  # Manually assign same worker to second station (simulating shared worker scenario)
82
- efr.login(station2)
82
+ efr.login(station2, None) # Login without worker first
83
83
  efr.assign(station2, auto_worker)
84
84
 
85
85
  initial_worker_count = len(efr._workers)
@@ -345,8 +345,8 @@ def test_assign_with_default_true():
345
345
 
346
346
  station = EventStation(key="test_station", respond_fn=handler)
347
347
 
348
- # Login without worker
349
- efr.login(station)
348
+ # Login without worker (pass None to explicitly not assign worker)
349
+ efr.login(station, None)
350
350
  assert station._worker is None, "Station should have no worker"
351
351
 
352
352
  # Assign with default (True) should auto-allocate
@@ -7,8 +7,7 @@ Integration Tests - End-to-end tests for the efr library.
7
7
  import unittest
8
8
  import time
9
9
  import threading
10
- from efr import EventFramework, Event, EventStation
11
- from efr.simple import EventSystem
10
+ from efr import EventFramework, Event, EventStation, EventSystem
12
11
 
13
12
 
14
13
  class TestIntegration(unittest.TestCase):
@@ -0,0 +1,140 @@
1
+ """
2
+ Test responsiveness with dt=1.0 second.
3
+
4
+ 验证 Event 优化后的响应性提升。
5
+ """
6
+ import time
7
+ import threading
8
+ from efr import EventFramework, Event, EventStation
9
+ from efr.utils.worker import Worker
10
+ from efr.utils.task import Task, CIRCLE
11
+
12
+
13
+ def test_worker_stop_responsiveness():
14
+ """Test worker stop() response time with mindt=1.0."""
15
+ print("\n" + "="*60)
16
+ print("Test 1: Worker stop() responsiveness (mindt=1.0s)")
17
+ print("="*60)
18
+
19
+ results = []
20
+
21
+ def worker_task():
22
+ results.append(time.time())
23
+
24
+ # Create worker with 1.0s mindt
25
+ worker = Worker(name="test_worker", mindt=1.0, always=True)
26
+ worker.add_task(Task(worker_task, times=CIRCLE))
27
+ worker.start()
28
+
29
+ # Wait for first execution
30
+ time.sleep(0.1)
31
+ initial_count = len(results)
32
+ print(f"Initial results count: {initial_count}")
33
+
34
+ # Start timing stop() response
35
+ start_time = time.time()
36
+ worker.stop()
37
+ worker.join(timeout=2.0) # Wait for thread to finish
38
+ end_time = time.time()
39
+
40
+ response_time = end_time - start_time
41
+ print(f"Stop response time: {response_time:.4f}s")
42
+ print(f"Expected (old sleep): ~1.0s")
43
+ print(f"Expected (new Event): ~0.0s")
44
+
45
+ if response_time < 0.1:
46
+ print("[PASS] Stop is immediate (using Event)")
47
+ else:
48
+ print("[FAIL] Stop has delay (still using sleep)")
49
+
50
+
51
+ def test_worker_add_task_responsiveness():
52
+ """Test add_task() immediate processing with mindt=1.0s."""
53
+ print("\n" + "="*60)
54
+ print("Test 2: Worker add_task() responsiveness (mindt=1.0s)")
55
+ print("="*60)
56
+
57
+ results = []
58
+ task_executed = threading.Event()
59
+
60
+ def new_task():
61
+ results.append(time.time())
62
+ task_executed.set()
63
+
64
+ # Create worker with 1.0s mindt
65
+ worker = Worker(name="test_worker2", mindt=1.0, always=True)
66
+ worker.start()
67
+
68
+ # Wait for worker to enter sleep
69
+ time.sleep(0.2)
70
+
71
+ # Add new task and measure response
72
+ start_time = time.time()
73
+ worker.add_task(Task(new_task, times=1))
74
+
75
+ # Wait for task to execute (with timeout)
76
+ task_executed.wait(timeout=2.0)
77
+ end_time = time.time()
78
+
79
+ response_time = end_time - start_time
80
+ print(f"Task execution response time: {response_time:.4f}s")
81
+ print(f"Expected (old sleep): ~1.0s (wait for next cycle)")
82
+ print(f"Expected (new Event): ~0.0s (immediate)")
83
+
84
+ if response_time < 0.1:
85
+ print("[PASS] New task processed immediately (using Event)")
86
+ else:
87
+ print("[FAIL] New task has delay (still using sleep)")
88
+
89
+ worker.stop()
90
+ worker.join(timeout=2.0)
91
+
92
+
93
+ def test_framework_stop_responsiveness():
94
+ """Test EventFramework quit() response time with timedt=1.0s."""
95
+ print("\n" + "="*60)
96
+ print("Test 3: EventFramework quit() responsiveness (timedt=1.0s)")
97
+ print("="*60)
98
+
99
+ # Create framework with 1.0s timedt
100
+ efr = EventFramework(name="test_framework", timedt=1.0)
101
+
102
+ def handler(event):
103
+ return {"status": "ok"}
104
+
105
+ station = EventStation(key="test", respond_fn=handler)
106
+ efr.login(station, True)
107
+ efr.start()
108
+
109
+ # Wait for framework to start
110
+ time.sleep(0.2)
111
+
112
+ # Measure quit() response
113
+ start_time = time.time()
114
+ efr.quit()
115
+ end_time = time.time()
116
+
117
+ response_time = end_time - start_time
118
+ print(f"Quit response time: {response_time:.4f}s")
119
+ print(f"Expected (old sleep): ~1.0s")
120
+ print(f"Expected (new Event): ~0.0s")
121
+
122
+ if response_time < 0.5: # Framework has more cleanup, allow slightly more time
123
+ print("[PASS] Quit is fast (using Event)")
124
+ else:
125
+ print("[FAIL] Quit has significant delay")
126
+
127
+
128
+ if __name__ == "__main__":
129
+ print("\n" + "="*60)
130
+ print("Responsiveness Test Suite")
131
+ print("Testing with dt=1.0 second")
132
+ print("="*60)
133
+
134
+ test_worker_stop_responsiveness()
135
+ test_worker_add_task_responsiveness()
136
+ test_framework_stop_responsiveness()
137
+
138
+ print("\n" + "="*60)
139
+ print("All tests completed!")
140
+ print("="*60)
@@ -67,6 +67,8 @@ class Worker(threading.Thread):
67
67
  self._tasks: List[Task] = []
68
68
  self._task_costs: Dict[Task, int] = {} # Track cost for each task (for auto mode)
69
69
  self._lock: threading.Lock = threading.Lock()
70
+ self._stop_event: threading.Event = threading.Event() # For stopping worker
71
+ self._wakeup_event: threading.Event = threading.Event() # For waking up early
70
72
 
71
73
  @property
72
74
  def auto_task_count(self) -> int:
@@ -80,13 +82,17 @@ class Worker(threading.Thread):
80
82
 
81
83
  def run(self) -> None:
82
84
  """Main worker loop."""
83
- while self.alive:
85
+ while not self._stop_event.is_set():
86
+ # Clear wakeup event at start of each iteration
87
+ # (add_task() may have set it to wake us up early)
88
+ self._wakeup_event.clear()
89
+
84
90
  next_time = time.time() + self.mindt
85
-
91
+
86
92
  # Execute all pending tasks
87
93
  with self._lock:
88
94
  tasks = self._tasks.copy()
89
-
95
+
90
96
  remaining: List[Task] = []
91
97
  for task in tasks:
92
98
  try:
@@ -94,26 +100,36 @@ class Worker(threading.Thread):
94
100
  except Exception as e:
95
101
  # Log error but continue
96
102
  print(f"Task error: {e}")
97
-
103
+
98
104
  if task.left > 0:
99
105
  remaining.append(task)
100
-
101
- if not self.alive:
106
+
107
+ if self._stop_event.is_set():
102
108
  break
103
-
109
+
104
110
  with self._lock:
105
111
  self._tasks = remaining
106
-
112
+
107
113
  # Auto-stop if not always and no tasks
108
114
  if not self.always and not self._tasks:
109
115
  self.alive = False
110
116
  break
111
-
112
- # Sleep until next iteration
117
+
118
+ # Wait until next iteration (can be woken up early)
113
119
  sleep_time = next_time - time.time()
114
120
  if sleep_time > 0:
115
- time.sleep(sleep_time)
116
-
121
+ # Wait on wakeup_event (can be set by add_task or stop())
122
+ # If set early, we'll immediately continue to next iteration
123
+ if self._wakeup_event.wait(sleep_time):
124
+ # wakeup_event was set, check if stopping
125
+ self._wakeup_event.clear()
126
+ if self._stop_event.is_set():
127
+ break # Exit loop
128
+ continue # New task, process immediately
129
+ # Otherwise timeout occurred, normal iteration
130
+
131
+ self.alive = False
132
+
117
133
  def add_task(self, task: Task, cost: int = 1) -> bool:
118
134
  """
119
135
  Add a task to the worker.
@@ -129,8 +145,10 @@ class Worker(threading.Thread):
129
145
  self._tasks.append(task)
130
146
  if self.auto_managed:
131
147
  self._task_costs[task] = cost
148
+ # Wake up worker immediately to process new task
149
+ self._wakeup_event.set()
132
150
  return True
133
-
151
+
134
152
  def remove_task(self, task: Task) -> bool:
135
153
  """
136
154
  Remove a task from the worker.
@@ -151,7 +169,8 @@ class Worker(threading.Thread):
151
169
 
152
170
  def stop(self) -> None:
153
171
  """Signal the worker to stop."""
154
- self.alive = False
172
+ self._stop_event.set()
173
+ self._wakeup_event.set() # Wake up worker if sleeping
155
174
 
156
175
  def has_tasks(self) -> bool:
157
176
  """Check if worker has pending tasks."""
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: efr
3
- Version: 0.3.2
3
+ Version: 0.3.3
4
4
  Summary: ...
5
5
  Author-email: 2229066748@qq.com
6
6
  Maintainer: Eagle'sBaby
@@ -31,6 +31,7 @@ efr/test/test_framework.py
31
31
  efr/test/test_integration.py
32
32
  efr/test/test_queue.py
33
33
  efr/test/test_readme_examples.py
34
+ efr/test/test_responsiveness.py
34
35
  efr/test/test_station.py
35
36
  efr/test/test_task.py
36
37
  efr/test/test_worker.py
@@ -9,7 +9,7 @@ from setuptools.command.install import install
9
9
 
10
10
  setup(
11
11
  name='efr',
12
- version='0.3.2',
12
+ version='0.3.3',
13
13
  description='...',
14
14
  author_email='2229066748@qq.com',
15
15
  maintainer="Eagle'sBaby",
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes