parallelazy 0.1.0__tar.gz → 0.1.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.
- {parallelazy-0.1.0 → parallelazy-0.1.2}/PKG-INFO +18 -1
- {parallelazy-0.1.0 → parallelazy-0.1.2}/README.md +17 -0
- {parallelazy-0.1.0 → parallelazy-0.1.2}/parallelazy/__init__.py +17 -0
- {parallelazy-0.1.0 → parallelazy-0.1.2}/parallelazy/environment_manager.py +34 -0
- {parallelazy-0.1.0 → parallelazy-0.1.2}/parallelazy/plugin_logging.py +153 -4
- {parallelazy-0.1.0 → parallelazy-0.1.2}/parallelazy/plugin_manager.py +168 -7
- parallelazy-0.1.2/parallelazy/plugin_monitor.py +725 -0
- {parallelazy-0.1.0 → parallelazy-0.1.2}/parallelazy/plugin_worker.py +5 -0
- {parallelazy-0.1.0 → parallelazy-0.1.2}/parallelazy.egg-info/PKG-INFO +18 -1
- {parallelazy-0.1.0 → parallelazy-0.1.2}/parallelazy.egg-info/SOURCES.txt +3 -1
- {parallelazy-0.1.0 → parallelazy-0.1.2}/pyproject.toml +1 -1
- {parallelazy-0.1.0 → parallelazy-0.1.2}/tests/test_plugin_lifecycle.py +24 -0
- {parallelazy-0.1.0 → parallelazy-0.1.2}/tests/test_plugin_logging.py +141 -1
- parallelazy-0.1.2/tests/test_plugin_monitoring.py +276 -0
- {parallelazy-0.1.0 → parallelazy-0.1.2}/LICENSE +0 -0
- {parallelazy-0.1.0 → parallelazy-0.1.2}/parallelazy/plugin.py +0 -0
- {parallelazy-0.1.0 → parallelazy-0.1.2}/parallelazy/plugin_data.py +0 -0
- {parallelazy-0.1.0 → parallelazy-0.1.2}/parallelazy/plugin_rpc.py +0 -0
- {parallelazy-0.1.0 → parallelazy-0.1.2}/parallelazy/plugin_transport.py +0 -0
- {parallelazy-0.1.0 → parallelazy-0.1.2}/parallelazy/utils.py +0 -0
- {parallelazy-0.1.0 → parallelazy-0.1.2}/parallelazy.egg-info/dependency_links.txt +0 -0
- {parallelazy-0.1.0 → parallelazy-0.1.2}/parallelazy.egg-info/requires.txt +0 -0
- {parallelazy-0.1.0 → parallelazy-0.1.2}/parallelazy.egg-info/top_level.txt +0 -0
- {parallelazy-0.1.0 → parallelazy-0.1.2}/setup.cfg +0 -0
- {parallelazy-0.1.0 → parallelazy-0.1.2}/tests/test_env_cuda_ipc.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: parallelazy
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.2
|
|
4
4
|
Summary: A lightweight multiprocess worker-plugin framework. Topic-based pub/sub, global enviroment, using process spawn and pipes, pytorch and CUDA ready.
|
|
5
5
|
Author: Cesare Montresor
|
|
6
6
|
Project-URL: Homepage, https://github.com/cesare-montresor/parallelazy
|
|
@@ -81,6 +81,23 @@ if __name__ == "__main__":
|
|
|
81
81
|
main()
|
|
82
82
|
```
|
|
83
83
|
|
|
84
|
+
## Pipeline monitoring
|
|
85
|
+
|
|
86
|
+
Enable an optional read-only view of topic backlog, plugin process resources,
|
|
87
|
+
and shared environment activity:
|
|
88
|
+
|
|
89
|
+
```python
|
|
90
|
+
manager.enable_monitoring(sample_interval=1.0)
|
|
91
|
+
snapshot = manager.get_monitoring_snapshot()
|
|
92
|
+
manager.print_monitoring_report()
|
|
93
|
+
manager.disable_monitoring()
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
The monitor never reads or changes topic payloads. Topic backlog is reported as
|
|
97
|
+
pending pipe bytes. Process metrics use `psutil` when installed, and NVIDIA GPU
|
|
98
|
+
metrics use `nvidia-ml-py`; missing optional dependencies are identified with
|
|
99
|
+
their install command.
|
|
100
|
+
|
|
84
101
|
## Documentation
|
|
85
102
|
|
|
86
103
|
Read the [documentation index](docs/index.md) for installation, concepts,
|
|
@@ -65,6 +65,23 @@ if __name__ == "__main__":
|
|
|
65
65
|
main()
|
|
66
66
|
```
|
|
67
67
|
|
|
68
|
+
## Pipeline monitoring
|
|
69
|
+
|
|
70
|
+
Enable an optional read-only view of topic backlog, plugin process resources,
|
|
71
|
+
and shared environment activity:
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
manager.enable_monitoring(sample_interval=1.0)
|
|
75
|
+
snapshot = manager.get_monitoring_snapshot()
|
|
76
|
+
manager.print_monitoring_report()
|
|
77
|
+
manager.disable_monitoring()
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
The monitor never reads or changes topic payloads. Topic backlog is reported as
|
|
81
|
+
pending pipe bytes. Process metrics use `psutil` when installed, and NVIDIA GPU
|
|
82
|
+
metrics use `nvidia-ml-py`; missing optional dependencies are identified with
|
|
83
|
+
their install command.
|
|
84
|
+
|
|
68
85
|
## Documentation
|
|
69
86
|
|
|
70
87
|
Read the [documentation index](docs/index.md) for installation, concepts,
|
|
@@ -66,6 +66,16 @@ from parallelazy.plugin_transport import (
|
|
|
66
66
|
mark_graph_output,
|
|
67
67
|
sanitize_for_ipc,
|
|
68
68
|
)
|
|
69
|
+
from parallelazy.utils import is_debugpy_attached
|
|
70
|
+
from parallelazy.plugin_monitor import (
|
|
71
|
+
CapabilityAvailability,
|
|
72
|
+
EnvironmentMetric,
|
|
73
|
+
MonitoringSnapshot,
|
|
74
|
+
PipelineMonitor,
|
|
75
|
+
ProcessMetric,
|
|
76
|
+
TopicMetric,
|
|
77
|
+
)
|
|
78
|
+
|
|
69
79
|
from parallelazy.plugin_worker import PluginWorker
|
|
70
80
|
|
|
71
81
|
__all__ = [
|
|
@@ -119,4 +129,11 @@ __all__ = [
|
|
|
119
129
|
"IpcEventPoolManaged",
|
|
120
130
|
"mark_graph_output",
|
|
121
131
|
"sanitize_for_ipc",
|
|
132
|
+
"is_debugpy_attached",
|
|
133
|
+
"CapabilityAvailability",
|
|
134
|
+
"EnvironmentMetric",
|
|
135
|
+
"MonitoringSnapshot",
|
|
136
|
+
"PipelineMonitor",
|
|
137
|
+
"ProcessMetric",
|
|
138
|
+
"TopicMetric",
|
|
122
139
|
]
|
|
@@ -8,6 +8,7 @@ and broadcasting updates to interested plugins.
|
|
|
8
8
|
from typing import Any, Dict
|
|
9
9
|
from threading import Lock
|
|
10
10
|
import logging
|
|
11
|
+
import time
|
|
11
12
|
|
|
12
13
|
logger = logging.getLogger(__name__)
|
|
13
14
|
|
|
@@ -20,6 +21,9 @@ class EnvironmentManager:
|
|
|
20
21
|
"""Initialize environment manager."""
|
|
21
22
|
self._env: Dict[str, Any] = {}
|
|
22
23
|
self._lock = Lock()
|
|
24
|
+
self._monitoring_enabled = False
|
|
25
|
+
self._write_counts: Dict[str, int] = {}
|
|
26
|
+
self._last_write_times: Dict[str, float] = {}
|
|
23
27
|
|
|
24
28
|
def get(self, key: str, default: Any = None) -> Any:
|
|
25
29
|
"""Get environment variable value."""
|
|
@@ -30,6 +34,36 @@ class EnvironmentManager:
|
|
|
30
34
|
"""Set environment variable."""
|
|
31
35
|
with self._lock:
|
|
32
36
|
self._env[key] = value
|
|
37
|
+
if self._monitoring_enabled:
|
|
38
|
+
self._write_counts[key] = self._write_counts.get(key, 0) + 1
|
|
39
|
+
self._last_write_times[key] = time.monotonic()
|
|
40
|
+
|
|
41
|
+
def begin_monitoring(self) -> None:
|
|
42
|
+
"""Start collecting environment write counters."""
|
|
43
|
+
with self._lock:
|
|
44
|
+
self._monitoring_enabled = True
|
|
45
|
+
self._write_counts = {key: 0 for key in self._env}
|
|
46
|
+
self._last_write_times = {}
|
|
47
|
+
|
|
48
|
+
def end_monitoring(self) -> None:
|
|
49
|
+
"""Stop collecting environment write counters."""
|
|
50
|
+
with self._lock:
|
|
51
|
+
self._monitoring_enabled = False
|
|
52
|
+
self._write_counts = {}
|
|
53
|
+
self._last_write_times = {}
|
|
54
|
+
|
|
55
|
+
def monitoring_snapshot(self) -> Dict[str, Dict[str, Any]]:
|
|
56
|
+
"""Return environment metadata and write counters without values."""
|
|
57
|
+
with self._lock:
|
|
58
|
+
return {
|
|
59
|
+
key: {
|
|
60
|
+
"type": type(value).__name__,
|
|
61
|
+
"stored_size": __import__("sys").getsizeof(value),
|
|
62
|
+
"write_count": self._write_counts.get(key, 0),
|
|
63
|
+
"last_write": self._last_write_times.get(key),
|
|
64
|
+
}
|
|
65
|
+
for key, value in self._env.items()
|
|
66
|
+
}
|
|
33
67
|
|
|
34
68
|
def get_all(self) -> Dict[str, Any]:
|
|
35
69
|
"""Get all environment variables."""
|
|
@@ -5,7 +5,9 @@ from datetime import datetime, timezone
|
|
|
5
5
|
import hashlib
|
|
6
6
|
import logging
|
|
7
7
|
import os
|
|
8
|
+
import re
|
|
8
9
|
import sys
|
|
10
|
+
import threading
|
|
9
11
|
import time
|
|
10
12
|
from typing import List, Optional, TextIO
|
|
11
13
|
|
|
@@ -59,6 +61,128 @@ def default_runtime_settings() -> PluginRuntimeSettings:
|
|
|
59
61
|
)
|
|
60
62
|
|
|
61
63
|
|
|
64
|
+
class _CapturedStreamConsoleFilter(logging.Filter):
|
|
65
|
+
def filter(self, record: logging.LogRecord) -> bool:
|
|
66
|
+
return not getattr(record, "_parallelazy_captured_stream", False)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class _TeeTextStream:
|
|
70
|
+
"""Mirror worker stream writes to the original stream and its log file."""
|
|
71
|
+
|
|
72
|
+
def __init__(self, stream: TextIO, logger: logging.Logger, label: str):
|
|
73
|
+
self._stream = stream
|
|
74
|
+
self._logger = logger
|
|
75
|
+
self._label = label
|
|
76
|
+
self._buffer = ""
|
|
77
|
+
self._lock = threading.RLock()
|
|
78
|
+
self._logging = threading.local()
|
|
79
|
+
|
|
80
|
+
@property
|
|
81
|
+
def encoding(self):
|
|
82
|
+
return self._stream.encoding
|
|
83
|
+
|
|
84
|
+
@property
|
|
85
|
+
def errors(self):
|
|
86
|
+
return self._stream.errors
|
|
87
|
+
|
|
88
|
+
@property
|
|
89
|
+
def buffer(self):
|
|
90
|
+
return self._stream.buffer
|
|
91
|
+
|
|
92
|
+
@property
|
|
93
|
+
def name(self):
|
|
94
|
+
return self._stream.name
|
|
95
|
+
|
|
96
|
+
@property
|
|
97
|
+
def closed(self):
|
|
98
|
+
return self._stream.closed
|
|
99
|
+
|
|
100
|
+
def isatty(self) -> bool:
|
|
101
|
+
return self._stream.isatty()
|
|
102
|
+
|
|
103
|
+
def fileno(self) -> int:
|
|
104
|
+
return self._stream.fileno()
|
|
105
|
+
|
|
106
|
+
def writable(self) -> bool:
|
|
107
|
+
return self._stream.writable()
|
|
108
|
+
|
|
109
|
+
def write(self, text: str) -> int:
|
|
110
|
+
if not isinstance(text, str):
|
|
111
|
+
raise TypeError("write() argument must be str")
|
|
112
|
+
with self._lock:
|
|
113
|
+
written = self._stream.write(text)
|
|
114
|
+
if "\r" in text or "\n" in text:
|
|
115
|
+
self._stream.flush()
|
|
116
|
+
if not getattr(self._logging, "active", False):
|
|
117
|
+
self._capture_complete_lines(text)
|
|
118
|
+
return written
|
|
119
|
+
|
|
120
|
+
def _capture_complete_lines(self, text: str) -> None:
|
|
121
|
+
for index, character in enumerate(text):
|
|
122
|
+
if character in ("\r", "\n"):
|
|
123
|
+
if character == "\n" and index > 0 and text[index - 1] == "\r":
|
|
124
|
+
continue
|
|
125
|
+
self._emit_buffer()
|
|
126
|
+
else:
|
|
127
|
+
self._buffer += character
|
|
128
|
+
|
|
129
|
+
def _emit_buffer(self) -> None:
|
|
130
|
+
line = self._buffer
|
|
131
|
+
self._buffer = ""
|
|
132
|
+
self._log_line(line)
|
|
133
|
+
|
|
134
|
+
def _log_line(self, line: str) -> None:
|
|
135
|
+
self._logging.active = True
|
|
136
|
+
try:
|
|
137
|
+
self._logger.info(
|
|
138
|
+
"[%s] %s",
|
|
139
|
+
self._label,
|
|
140
|
+
line,
|
|
141
|
+
extra={"_parallelazy_captured_stream": True},
|
|
142
|
+
)
|
|
143
|
+
finally:
|
|
144
|
+
self._logging.active = False
|
|
145
|
+
|
|
146
|
+
def flush(self) -> None:
|
|
147
|
+
with self._lock:
|
|
148
|
+
self._stream.flush()
|
|
149
|
+
if self._buffer and not getattr(self._logging, "active", False):
|
|
150
|
+
self._emit_buffer()
|
|
151
|
+
|
|
152
|
+
def close(self) -> None:
|
|
153
|
+
self.flush()
|
|
154
|
+
|
|
155
|
+
def __getattr__(self, name):
|
|
156
|
+
return getattr(self._stream, name)
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
class WorkerOutputCapture:
|
|
160
|
+
"""Restore worker streams after teeing their output into the worker log."""
|
|
161
|
+
|
|
162
|
+
def __init__(self, logger: logging.Logger):
|
|
163
|
+
self._original_stdout = sys.stdout
|
|
164
|
+
self._original_stderr = sys.stderr
|
|
165
|
+
self.stdout = _TeeTextStream(self._original_stdout, logger, "stdout")
|
|
166
|
+
self.stderr = _TeeTextStream(self._original_stderr, logger, "stderr")
|
|
167
|
+
sys.stdout = self.stdout
|
|
168
|
+
sys.stderr = self.stderr
|
|
169
|
+
|
|
170
|
+
def close(self) -> None:
|
|
171
|
+
try:
|
|
172
|
+
self.stdout.flush()
|
|
173
|
+
self.stderr.flush()
|
|
174
|
+
finally:
|
|
175
|
+
if sys.stdout is self.stdout:
|
|
176
|
+
sys.stdout = self._original_stdout
|
|
177
|
+
if sys.stderr is self.stderr:
|
|
178
|
+
sys.stderr = self._original_stderr
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
def configure_worker_output(logger: logging.Logger) -> WorkerOutputCapture:
|
|
182
|
+
"""Mirror worker stdout and stderr to both the terminal and plugin log."""
|
|
183
|
+
return WorkerOutputCapture(logger)
|
|
184
|
+
|
|
185
|
+
|
|
62
186
|
def _format_elapsed(seconds: float) -> str:
|
|
63
187
|
milliseconds = max(0, int(seconds * 1000))
|
|
64
188
|
hours, remainder = divmod(milliseconds, 3_600_000)
|
|
@@ -94,9 +218,9 @@ class _RecoveringFileHandler(logging.Handler):
|
|
|
94
218
|
self.stream: Optional[TextIO] = None
|
|
95
219
|
self._open_stream()
|
|
96
220
|
|
|
97
|
-
def
|
|
98
|
-
os.makedirs(os.path.dirname(
|
|
99
|
-
stream = open(
|
|
221
|
+
def _create_stream(self, filename: str) -> TextIO:
|
|
222
|
+
os.makedirs(os.path.dirname(filename), exist_ok=True)
|
|
223
|
+
stream = open(filename, "a", encoding="utf-8")
|
|
100
224
|
try:
|
|
101
225
|
stream.write(f"# Log opened: {_utc_timestamp()}{self.terminator}")
|
|
102
226
|
stream.write(
|
|
@@ -107,7 +231,26 @@ class _RecoveringFileHandler(logging.Handler):
|
|
|
107
231
|
except Exception:
|
|
108
232
|
stream.close()
|
|
109
233
|
raise
|
|
110
|
-
|
|
234
|
+
return stream
|
|
235
|
+
|
|
236
|
+
def _open_stream(self) -> None:
|
|
237
|
+
self.stream = self._create_stream(self.base_filename)
|
|
238
|
+
|
|
239
|
+
def set_filename(self, filename: str) -> None:
|
|
240
|
+
filename = os.path.abspath(filename)
|
|
241
|
+
if filename == self.base_filename:
|
|
242
|
+
return
|
|
243
|
+
|
|
244
|
+
stream = self._create_stream(filename)
|
|
245
|
+
self.acquire()
|
|
246
|
+
try:
|
|
247
|
+
previous_stream = self.stream
|
|
248
|
+
self.base_filename = filename
|
|
249
|
+
self.stream = stream
|
|
250
|
+
if previous_stream is not None:
|
|
251
|
+
previous_stream.close()
|
|
252
|
+
finally:
|
|
253
|
+
self.release()
|
|
111
254
|
|
|
112
255
|
def _close_stream(self) -> None:
|
|
113
256
|
stream = self.stream
|
|
@@ -225,6 +368,7 @@ class LoggingSession:
|
|
|
225
368
|
console_handler = logging.StreamHandler(sys.stdout)
|
|
226
369
|
console_handler.setLevel(log_level)
|
|
227
370
|
console_handler.addFilter(self._context_filter)
|
|
371
|
+
console_handler.addFilter(_CapturedStreamConsoleFilter())
|
|
228
372
|
console_handler.setFormatter(
|
|
229
373
|
_make_formatter(self.console_format, CONSOLE_DATE_FORMAT)
|
|
230
374
|
)
|
|
@@ -252,6 +396,11 @@ class LoggingSession:
|
|
|
252
396
|
self.file_format = new_file_format
|
|
253
397
|
self.console_format = new_console_format
|
|
254
398
|
|
|
399
|
+
def set_log_file(self, log_file: str) -> None:
|
|
400
|
+
for handler in self._handlers:
|
|
401
|
+
if isinstance(handler, _RecoveringFileHandler):
|
|
402
|
+
handler.set_filename(log_file)
|
|
403
|
+
|
|
255
404
|
def close(self) -> None:
|
|
256
405
|
for handler in self._handlers:
|
|
257
406
|
self.logger.removeHandler(handler)
|
|
@@ -77,13 +77,14 @@ class PluginManager:
|
|
|
77
77
|
if isinstance(log_level, bool) or not isinstance(log_level, int):
|
|
78
78
|
raise TypeError("log_level must be an integer logging level")
|
|
79
79
|
|
|
80
|
-
self.
|
|
81
|
-
self.
|
|
80
|
+
self._work_dir = resolve_directory(work_dir or os.getcwd())
|
|
81
|
+
self._log_dir_setting = log_dir
|
|
82
|
+
self._log_dir = resolve_directory(log_dir, self._work_dir)
|
|
82
83
|
self.log_level = log_level
|
|
83
84
|
self.display_logs = display_logs
|
|
84
85
|
self.run_id = create_run_id()
|
|
85
86
|
self.run_log_dir = os.path.join(self.log_dir, self.run_id)
|
|
86
|
-
os.makedirs(self.
|
|
87
|
+
os.makedirs(self._work_dir, exist_ok=True)
|
|
87
88
|
os.makedirs(self.run_log_dir, exist_ok=True)
|
|
88
89
|
|
|
89
90
|
self._logging = configure_manager_logging(
|
|
@@ -103,6 +104,8 @@ class PluginManager:
|
|
|
103
104
|
self._env_subscribers: Dict[str, List[str]] = {} # env_key -> [plugin_id, ...]
|
|
104
105
|
|
|
105
106
|
self._env = EnvironmentManager()
|
|
107
|
+
self._monitor_lock = threading.Lock()
|
|
108
|
+
self._monitor = None
|
|
106
109
|
|
|
107
110
|
self.timeout_request = timeout_request
|
|
108
111
|
self.worker_pydebug = is_debugpy_attached()
|
|
@@ -112,6 +115,51 @@ class PluginManager:
|
|
|
112
115
|
if self.worker_pydebug:
|
|
113
116
|
setup_worker_debugging_env()
|
|
114
117
|
|
|
118
|
+
@property
|
|
119
|
+
def work_dir(self) -> str:
|
|
120
|
+
"""Base working directory for the manager and its plugins."""
|
|
121
|
+
return self._work_dir
|
|
122
|
+
|
|
123
|
+
@property
|
|
124
|
+
def log_dir(self) -> str:
|
|
125
|
+
"""Base directory for manager and plugin log files."""
|
|
126
|
+
return self._log_dir
|
|
127
|
+
|
|
128
|
+
@log_dir.setter
|
|
129
|
+
def log_dir(self, path: str) -> None:
|
|
130
|
+
log_dir = resolve_directory(path, self.work_dir)
|
|
131
|
+
with self._plugins_lock:
|
|
132
|
+
if log_dir == self._log_dir:
|
|
133
|
+
self._log_dir_setting = path
|
|
134
|
+
return
|
|
135
|
+
if self._plugins or self._loading_plugins:
|
|
136
|
+
raise RuntimeError("log_dir cannot be changed after plugin loading starts")
|
|
137
|
+
|
|
138
|
+
self._update_log_dir(log_dir)
|
|
139
|
+
self._log_dir_setting = path
|
|
140
|
+
|
|
141
|
+
@work_dir.setter
|
|
142
|
+
def work_dir(self, path: str) -> None:
|
|
143
|
+
work_dir = resolve_directory(path)
|
|
144
|
+
if work_dir == self._work_dir:
|
|
145
|
+
return
|
|
146
|
+
|
|
147
|
+
with self._plugins_lock:
|
|
148
|
+
if self._plugins or self._loading_plugins:
|
|
149
|
+
raise RuntimeError("work_dir cannot be changed after plugin loading starts")
|
|
150
|
+
|
|
151
|
+
log_dir = resolve_directory(self._log_dir_setting, work_dir)
|
|
152
|
+
os.makedirs(work_dir, exist_ok=True)
|
|
153
|
+
self._update_log_dir(log_dir)
|
|
154
|
+
self._work_dir = work_dir
|
|
155
|
+
|
|
156
|
+
def _update_log_dir(self, log_dir: str) -> None:
|
|
157
|
+
run_log_dir = os.path.join(log_dir, self.run_id)
|
|
158
|
+
os.makedirs(run_log_dir, exist_ok=True)
|
|
159
|
+
self._logging.set_log_file(os.path.join(run_log_dir, "manager.log"))
|
|
160
|
+
self._log_dir = log_dir
|
|
161
|
+
self.run_log_dir = run_log_dir
|
|
162
|
+
|
|
115
163
|
def set_log_formats(
|
|
116
164
|
self,
|
|
117
165
|
file_format: Optional[str] = None,
|
|
@@ -318,6 +366,32 @@ class PluginManager:
|
|
|
318
366
|
except Exception as e:
|
|
319
367
|
self.logger.error(f"Error starting plugin {plugin_id}: {e}")
|
|
320
368
|
return False
|
|
369
|
+
|
|
370
|
+
def start_plugins(self, plugin_ids: List[str], parallel: bool = True) -> None:
|
|
371
|
+
"""
|
|
372
|
+
Start multiple loaded plugins.
|
|
373
|
+
|
|
374
|
+
Args:
|
|
375
|
+
plugin_ids: Identifiers of the plugins to start
|
|
376
|
+
parallel: Whether to start plugins in parallel
|
|
377
|
+
"""
|
|
378
|
+
if parallel:
|
|
379
|
+
barrier = mp.Barrier(len(plugin_ids) + 1)
|
|
380
|
+
for plugin_id in plugin_ids:
|
|
381
|
+
thread = threading.Thread(
|
|
382
|
+
target=self._start_plugin_thread,
|
|
383
|
+
args=(plugin_id, barrier),
|
|
384
|
+
)
|
|
385
|
+
thread.start()
|
|
386
|
+
barrier.wait()
|
|
387
|
+
else:
|
|
388
|
+
for plugin_id in plugin_ids:
|
|
389
|
+
self.start_plugin(plugin_id)
|
|
390
|
+
|
|
391
|
+
def _start_plugin_thread(self, plugin_id: str, barrier: 'mp.Barrier') -> None:
|
|
392
|
+
"""Start a plugin in a thread and wait for the other starts to finish."""
|
|
393
|
+
self.start_plugin(plugin_id)
|
|
394
|
+
barrier.wait()
|
|
321
395
|
|
|
322
396
|
|
|
323
397
|
def stop_plugin(self, plugin_id: str) -> bool:
|
|
@@ -366,6 +440,7 @@ class PluginManager:
|
|
|
366
440
|
True if unloaded successfully
|
|
367
441
|
"""
|
|
368
442
|
try:
|
|
443
|
+
self.disable_monitoring()
|
|
369
444
|
if plugin_id in self._plugins:
|
|
370
445
|
info = self._plugins[plugin_id]
|
|
371
446
|
if info.process.is_alive() and not info.stop_confirmed:
|
|
@@ -415,10 +490,10 @@ class PluginManager:
|
|
|
415
490
|
if publisher == plugin_id:
|
|
416
491
|
del self._topic_publishers[topic]
|
|
417
492
|
|
|
418
|
-
def start_all_plugins(self) -> None:
|
|
493
|
+
def start_all_plugins(self, parallel=True) -> None:
|
|
419
494
|
"""Start all loaded plugins."""
|
|
420
|
-
|
|
421
|
-
|
|
495
|
+
ids = list(self._plugins.keys())
|
|
496
|
+
self.start_plugins(ids, parallel=parallel)
|
|
422
497
|
|
|
423
498
|
def stop_all_plugins(self) -> None:
|
|
424
499
|
"""Stop all running plugins."""
|
|
@@ -428,6 +503,7 @@ class PluginManager:
|
|
|
428
503
|
|
|
429
504
|
def unload_all_plugins(self) -> None:
|
|
430
505
|
"""Shut down the plugin manager and all plugins."""
|
|
506
|
+
self.disable_monitoring()
|
|
431
507
|
self.stop_all_plugins()
|
|
432
508
|
for plugin_id in list(self._plugins.keys()):
|
|
433
509
|
self.unload_plugin(plugin_id)
|
|
@@ -492,6 +568,91 @@ class PluginManager:
|
|
|
492
568
|
def plugin_list(self) -> List[str]:
|
|
493
569
|
"""List all loaded plugin IDs."""
|
|
494
570
|
return list(self._plugins.keys())
|
|
571
|
+
|
|
572
|
+
def enable_monitoring(
|
|
573
|
+
self,
|
|
574
|
+
sample_interval: float = 1.0,
|
|
575
|
+
):
|
|
576
|
+
"""Enable optional, read-only pipeline monitoring."""
|
|
577
|
+
from parallelazy.plugin_monitor import MonitoringAdapter, PipelineMonitor
|
|
578
|
+
|
|
579
|
+
if isinstance(sample_interval, bool) or not isinstance(sample_interval, (int, float)):
|
|
580
|
+
raise TypeError("sample_interval must be a number")
|
|
581
|
+
if sample_interval <= 0:
|
|
582
|
+
raise ValueError("sample_interval must be greater than zero")
|
|
583
|
+
|
|
584
|
+
with self._monitor_lock:
|
|
585
|
+
if self._monitor is not None:
|
|
586
|
+
self._monitor.start()
|
|
587
|
+
return self._monitor
|
|
588
|
+
|
|
589
|
+
monitor = PipelineMonitor(
|
|
590
|
+
MonitoringAdapter(self),
|
|
591
|
+
sample_interval=sample_interval,
|
|
592
|
+
logger=self.logger,
|
|
593
|
+
)
|
|
594
|
+
self._monitor = monitor
|
|
595
|
+
|
|
596
|
+
self._env.begin_monitoring()
|
|
597
|
+
monitor.start()
|
|
598
|
+
return monitor
|
|
599
|
+
|
|
600
|
+
def disable_monitoring(self) -> None:
|
|
601
|
+
"""Synchronously stop pipeline monitoring."""
|
|
602
|
+
with self._monitor_lock:
|
|
603
|
+
monitor = self._monitor
|
|
604
|
+
if monitor is not None:
|
|
605
|
+
monitor.stop()
|
|
606
|
+
self._env.end_monitoring()
|
|
607
|
+
with self._monitor_lock:
|
|
608
|
+
self._monitor = None
|
|
609
|
+
|
|
610
|
+
def get_monitoring_snapshot(self):
|
|
611
|
+
"""Return a point-in-time monitoring snapshot."""
|
|
612
|
+
from parallelazy.plugin_monitor import MonitoringAdapter, PipelineMonitor
|
|
613
|
+
|
|
614
|
+
with self._monitor_lock:
|
|
615
|
+
monitor = self._monitor
|
|
616
|
+
if monitor is not None:
|
|
617
|
+
return monitor.get_snapshot()
|
|
618
|
+
|
|
619
|
+
monitor = PipelineMonitor(
|
|
620
|
+
MonitoringAdapter(self),
|
|
621
|
+
sample_interval=1.0,
|
|
622
|
+
logger=self.logger,
|
|
623
|
+
)
|
|
624
|
+
return monitor.sample()
|
|
625
|
+
|
|
626
|
+
def print_monitoring_report(self) -> None:
|
|
627
|
+
"""Print the latest monitoring snapshot without triggering a new sample."""
|
|
628
|
+
from parallelazy.plugin_monitor import MonitoringAdapter, PipelineMonitor
|
|
629
|
+
|
|
630
|
+
with self._monitor_lock:
|
|
631
|
+
monitor = self._monitor
|
|
632
|
+
if monitor is not None:
|
|
633
|
+
monitor.print_report()
|
|
634
|
+
return
|
|
635
|
+
|
|
636
|
+
monitor = PipelineMonitor(
|
|
637
|
+
MonitoringAdapter(self),
|
|
638
|
+
sample_interval=1.0,
|
|
639
|
+
logger=self.logger,
|
|
640
|
+
)
|
|
641
|
+
monitor.print_report(monitor.sample())
|
|
642
|
+
|
|
643
|
+
def enable_report_hotkey(self, key: str = "m") -> bool:
|
|
644
|
+
"""Enable a hotkey that prints monitoring reports in an interactive TTY."""
|
|
645
|
+
monitor = self._monitor
|
|
646
|
+
if monitor is None:
|
|
647
|
+
monitor = self.enable_monitoring()
|
|
648
|
+
return monitor.start_hotkey(key)
|
|
649
|
+
|
|
650
|
+
def disable_report_hotkey(self) -> None:
|
|
651
|
+
"""Disable the report hotkey and restore terminal input state."""
|
|
652
|
+
with self._monitor_lock:
|
|
653
|
+
monitor = self._monitor
|
|
654
|
+
if monitor is not None:
|
|
655
|
+
monitor.stop_hotkey()
|
|
495
656
|
|
|
496
657
|
|
|
497
658
|
|
|
@@ -732,7 +893,7 @@ class PluginManager:
|
|
|
732
893
|
if plugin_pub is not None:
|
|
733
894
|
forward_rpc = self._plugins[plugin_pub].rpc
|
|
734
895
|
remote_result = forward_rpc.forward(req)
|
|
735
|
-
self.logger.
|
|
896
|
+
self.logger.debug(f"topic_subscribe: {remote_result}" )
|
|
736
897
|
|
|
737
898
|
if not remote_result: return False
|
|
738
899
|
|