parallelazy 0.1.2__tar.gz → 0.2.0__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.2 → parallelazy-0.2.0}/PKG-INFO +18 -6
- {parallelazy-0.1.2 → parallelazy-0.2.0}/README.md +3 -2
- {parallelazy-0.1.2 → parallelazy-0.2.0}/parallelazy/__init__.py +3 -3
- {parallelazy-0.1.2 → parallelazy-0.2.0}/parallelazy/environment_manager.py +9 -5
- {parallelazy-0.1.2 → parallelazy-0.2.0}/parallelazy/plugin.py +44 -19
- {parallelazy-0.1.2 → parallelazy-0.2.0}/parallelazy/plugin_data.py +175 -27
- {parallelazy-0.1.2 → parallelazy-0.2.0}/parallelazy/plugin_logging.py +48 -18
- {parallelazy-0.1.2 → parallelazy-0.2.0}/parallelazy/plugin_manager.py +531 -192
- {parallelazy-0.1.2 → parallelazy-0.2.0}/parallelazy/plugin_monitor.py +15 -6
- {parallelazy-0.1.2 → parallelazy-0.2.0}/parallelazy/plugin_rpc.py +154 -54
- parallelazy-0.2.0/parallelazy/plugin_transport.py +350 -0
- parallelazy-0.2.0/parallelazy/plugin_worker.py +1296 -0
- {parallelazy-0.1.2 → parallelazy-0.2.0}/parallelazy/utils.py +5 -3
- {parallelazy-0.1.2 → parallelazy-0.2.0}/parallelazy.egg-info/PKG-INFO +18 -6
- {parallelazy-0.1.2 → parallelazy-0.2.0}/parallelazy.egg-info/SOURCES.txt +7 -1
- parallelazy-0.2.0/parallelazy.egg-info/requires.txt +6 -0
- parallelazy-0.2.0/pyproject.toml +45 -0
- parallelazy-0.2.0/tests/test_api_env.py +135 -0
- parallelazy-0.2.0/tests/test_api_ipc_transport.py +276 -0
- parallelazy-0.2.0/tests/test_api_lifecycle.py +188 -0
- parallelazy-0.2.0/tests/test_api_logging_and_monitoring.py +173 -0
- parallelazy-0.2.0/tests/test_api_pubsub.py +172 -0
- parallelazy-0.2.0/tests/test_api_rpc_and_errors.py +168 -0
- parallelazy-0.2.0/tests/test_chaos_and_resilience.py +261 -0
- {parallelazy-0.1.2 → parallelazy-0.2.0}/tests/test_plugin_lifecycle.py +244 -7
- {parallelazy-0.1.2 → parallelazy-0.2.0}/tests/test_plugin_logging.py +55 -0
- parallelazy-0.1.2/parallelazy/plugin_transport.py +0 -365
- parallelazy-0.1.2/parallelazy/plugin_worker.py +0 -869
- parallelazy-0.1.2/parallelazy.egg-info/requires.txt +0 -3
- parallelazy-0.1.2/pyproject.toml +0 -30
- parallelazy-0.1.2/tests/test_env_cuda_ipc.py +0 -41
- {parallelazy-0.1.2 → parallelazy-0.2.0}/LICENSE +0 -0
- {parallelazy-0.1.2 → parallelazy-0.2.0}/parallelazy.egg-info/dependency_links.txt +0 -0
- {parallelazy-0.1.2 → parallelazy-0.2.0}/parallelazy.egg-info/top_level.txt +0 -0
- {parallelazy-0.1.2 → parallelazy-0.2.0}/setup.cfg +0 -0
- {parallelazy-0.1.2 → parallelazy-0.2.0}/tests/test_plugin_monitoring.py +0 -0
|
@@ -1,17 +1,28 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: parallelazy
|
|
3
|
-
Version: 0.
|
|
4
|
-
Summary: A lightweight multiprocess worker-plugin framework. Topic-based pub/sub, global
|
|
3
|
+
Version: 0.2.0
|
|
4
|
+
Summary: A lightweight multiprocess worker-plugin framework. Topic-based pub/sub, global environment, using process spawn and pipes, PyTorch and CUDA ready.
|
|
5
5
|
Author: Cesare Montresor
|
|
6
|
+
License-Expression: MIT
|
|
6
7
|
Project-URL: Homepage, https://github.com/cesare-montresor/parallelazy
|
|
7
8
|
Project-URL: Repository, https://github.com/cesare-montresor/parallelazy
|
|
8
9
|
Project-URL: Issues, https://github.com/cesare-montresor/parallelazy/issues
|
|
9
|
-
|
|
10
|
+
Classifier: Development Status :: 4 - Beta
|
|
11
|
+
Classifier: Intended Audience :: Developers
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
|
|
18
|
+
Requires-Python: >=3.11
|
|
10
19
|
Description-Content-Type: text/markdown
|
|
11
20
|
License-File: LICENSE
|
|
12
|
-
Requires-Dist: debugpy
|
|
13
21
|
Requires-Dist: torch
|
|
14
22
|
Requires-Dist: numpy
|
|
23
|
+
Provides-Extra: monitoring
|
|
24
|
+
Requires-Dist: psutil; extra == "monitoring"
|
|
25
|
+
Requires-Dist: nvidia-ml-py; extra == "monitoring"
|
|
15
26
|
Dynamic: license-file
|
|
16
27
|
|
|
17
28
|
# parallelazy
|
|
@@ -107,6 +118,7 @@ The complete runnable examples are in
|
|
|
107
118
|
[examples/](examples/). For example:
|
|
108
119
|
|
|
109
120
|
```bash
|
|
110
|
-
python -m examples.
|
|
111
|
-
python -m examples.
|
|
121
|
+
python -m examples.example_quickstart
|
|
122
|
+
python -m examples.example_tensor_pipeline
|
|
123
|
+
python -m examples.example_dynamic_orchestrator
|
|
112
124
|
```
|
|
@@ -91,6 +91,7 @@ The complete runnable examples are in
|
|
|
91
91
|
[examples/](examples/). For example:
|
|
92
92
|
|
|
93
93
|
```bash
|
|
94
|
-
python -m examples.
|
|
95
|
-
python -m examples.
|
|
94
|
+
python -m examples.example_quickstart
|
|
95
|
+
python -m examples.example_tensor_pipeline
|
|
96
|
+
python -m examples.example_dynamic_orchestrator
|
|
96
97
|
```
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
"""
|
|
2
|
-
|
|
2
|
+
parallelazy Plugin System: Lightweight multiprocessing-based plugin architecture.
|
|
3
3
|
|
|
4
4
|
Provides modular plugin execution with pub/sub messaging, environment variables,
|
|
5
5
|
and service invocation.
|
|
@@ -25,6 +25,7 @@ from parallelazy.plugin_data import (
|
|
|
25
25
|
ProcessStatus,
|
|
26
26
|
Queue,
|
|
27
27
|
QueueDescriptor,
|
|
28
|
+
QueueReceive,
|
|
28
29
|
QueueRecive,
|
|
29
30
|
QueueSend,
|
|
30
31
|
Request,
|
|
@@ -61,7 +62,6 @@ from parallelazy.plugin_rpc import (
|
|
|
61
62
|
)
|
|
62
63
|
from parallelazy.plugin_transport import (
|
|
63
64
|
IpcEventCuda,
|
|
64
|
-
IpcEventPool,
|
|
65
65
|
IpcEventPoolManaged,
|
|
66
66
|
mark_graph_output,
|
|
67
67
|
sanitize_for_ipc,
|
|
@@ -91,6 +91,7 @@ __all__ = [
|
|
|
91
91
|
"ProcessStatus",
|
|
92
92
|
"Queue",
|
|
93
93
|
"QueueDescriptor",
|
|
94
|
+
"QueueReceive",
|
|
94
95
|
"QueueRecive",
|
|
95
96
|
"QueueSend",
|
|
96
97
|
"Request",
|
|
@@ -125,7 +126,6 @@ __all__ = [
|
|
|
125
126
|
"PluginTopicRPC",
|
|
126
127
|
"PluginWorker",
|
|
127
128
|
"IpcEventCuda",
|
|
128
|
-
"IpcEventPool",
|
|
129
129
|
"IpcEventPoolManaged",
|
|
130
130
|
"mark_graph_output",
|
|
131
131
|
"sanitize_for_ipc",
|
|
@@ -75,13 +75,17 @@ class EnvironmentManager:
|
|
|
75
75
|
for key, value in env_dict.items():
|
|
76
76
|
self.set(key, value)
|
|
77
77
|
|
|
78
|
-
def clear(self, key: str=None) -> None:
|
|
79
|
-
"""Clear a specific environment variable."""
|
|
78
|
+
def clear(self, key: str = None) -> None:
|
|
79
|
+
"""Clear a specific environment variable, or all variables when key is None."""
|
|
80
80
|
with self._lock:
|
|
81
|
-
if key
|
|
82
|
-
del self._env[key]
|
|
83
|
-
else:
|
|
81
|
+
if key is None:
|
|
84
82
|
self._env.clear()
|
|
83
|
+
self._write_counts.clear()
|
|
84
|
+
self._last_write_times.clear()
|
|
85
|
+
else:
|
|
86
|
+
self._env.pop(key, None)
|
|
87
|
+
self._write_counts.pop(key, None)
|
|
88
|
+
self._last_write_times.pop(key, None)
|
|
85
89
|
|
|
86
90
|
def __contains__(self, key: str) -> bool:
|
|
87
91
|
"""Check if key exists in environment."""
|
|
@@ -6,7 +6,7 @@ plugins must implement or can override.
|
|
|
6
6
|
"""
|
|
7
7
|
|
|
8
8
|
import logging
|
|
9
|
-
from typing import Any, Callable, Dict, Optional, TYPE_CHECKING, Self
|
|
9
|
+
from typing import Any, Callable, Dict, List, Optional, TYPE_CHECKING, Self
|
|
10
10
|
from enum import Enum
|
|
11
11
|
|
|
12
12
|
from parallelazy.utils import camel_to_kebab
|
|
@@ -45,7 +45,7 @@ class PluginDescriptor:
|
|
|
45
45
|
Attributes:
|
|
46
46
|
name: Unique plugin identifier
|
|
47
47
|
version: Semantic version (e.g., "1.0.0")
|
|
48
|
-
package: Full package path (e.g., "
|
|
48
|
+
package: Full package path (e.g., "parallelazy.plugins.my_plugin")
|
|
49
49
|
class_name: Plugin class name in the package
|
|
50
50
|
description: Human-readable description
|
|
51
51
|
author: Plugin author
|
|
@@ -64,6 +64,7 @@ class PluginDescriptor:
|
|
|
64
64
|
log_dir: Optional[str] = None
|
|
65
65
|
log_level: Optional[int] = None
|
|
66
66
|
display_logs: Optional[bool] = None
|
|
67
|
+
write_logs: Optional[bool] = None
|
|
67
68
|
|
|
68
69
|
def to_dict(self) -> Dict[str, Any]:
|
|
69
70
|
"""Convert descriptor to dictionary (JSON-serializable)."""
|
|
@@ -92,7 +93,7 @@ class PluginDescriptor:
|
|
|
92
93
|
|
|
93
94
|
class Plugin:
|
|
94
95
|
"""
|
|
95
|
-
Base class for all
|
|
96
|
+
Base class for all parallelazy plugins.
|
|
96
97
|
|
|
97
98
|
Plugins run in isolated processes and communicate via:
|
|
98
99
|
1. Control channel: Load/unload/configure/service commands
|
|
@@ -122,7 +123,9 @@ class Plugin:
|
|
|
122
123
|
key = plugin_name or cls.__name__
|
|
123
124
|
|
|
124
125
|
if key in cls._registry:
|
|
125
|
-
|
|
126
|
+
logging.getLogger(__name__).debug(
|
|
127
|
+
"Overwriting existing plugin registration for '%s'", key
|
|
128
|
+
)
|
|
126
129
|
|
|
127
130
|
cls._registry[key] = cls
|
|
128
131
|
|
|
@@ -143,10 +146,16 @@ class Plugin:
|
|
|
143
146
|
log_dir: Optional[str] = None,
|
|
144
147
|
log_level: Optional[int] = None,
|
|
145
148
|
display_logs: Optional[bool] = None,
|
|
149
|
+
write_logs: Optional[bool] = None,
|
|
146
150
|
) -> PluginDescriptor:
|
|
147
151
|
all_options = cls.default_options().copy()
|
|
148
152
|
all_options.update(options or {})
|
|
149
153
|
|
|
154
|
+
resolved_description = (
|
|
155
|
+
description
|
|
156
|
+
if description is not None
|
|
157
|
+
else (cls.__doc__.strip() if cls.__doc__ else "")
|
|
158
|
+
)
|
|
150
159
|
|
|
151
160
|
return PluginDescriptor(
|
|
152
161
|
version=version,
|
|
@@ -155,11 +164,12 @@ class Plugin:
|
|
|
155
164
|
name= camel_to_kebab(name or cls.__name__),
|
|
156
165
|
package=f"{cls.__module__}",
|
|
157
166
|
class_name=cls.__name__,
|
|
158
|
-
description=
|
|
167
|
+
description=resolved_description,
|
|
159
168
|
work_dir=work_dir,
|
|
160
169
|
log_dir=log_dir,
|
|
161
170
|
log_level=log_level,
|
|
162
171
|
display_logs=display_logs,
|
|
172
|
+
write_logs=write_logs,
|
|
163
173
|
|
|
164
174
|
)
|
|
165
175
|
|
|
@@ -235,34 +245,49 @@ class Plugin:
|
|
|
235
245
|
"""Request plugin shutdown; use wait_for_stop() in blocking main() work."""
|
|
236
246
|
self._worker.stop()
|
|
237
247
|
|
|
238
|
-
def topic_subscribe(self, topic:str, callback:Callable) ->
|
|
239
|
-
"""Subscribe to a data topic
|
|
240
|
-
self._worker.topic_subscribe(topic, callback)
|
|
248
|
+
def topic_subscribe(self, topic: str, callback: Callable) -> bool:
|
|
249
|
+
"""Subscribe to a data topic."""
|
|
250
|
+
return self._worker.topic_subscribe(topic, callback)
|
|
251
|
+
|
|
252
|
+
def topic_unsubscribe(self, topic: str) -> bool:
|
|
253
|
+
"""Unsubscribe from a data topic."""
|
|
254
|
+
return self._worker.topic_unsubscribe(topic)
|
|
255
|
+
|
|
256
|
+
def topic_publish(self, topic: str, data: Any) -> int:
|
|
257
|
+
"""Publish data to a topic. Returns the number of subscribers that received the message."""
|
|
258
|
+
return self._worker.topic_publish(topic, data)
|
|
241
259
|
|
|
242
|
-
def
|
|
243
|
-
"""
|
|
244
|
-
self._worker.
|
|
260
|
+
def topic_list(self, plugin_id: Optional[str] = None) -> List[str]:
|
|
261
|
+
"""List available topics, or topics published by a specific plugin."""
|
|
262
|
+
return self._worker.topic_list(plugin_id=plugin_id)
|
|
245
263
|
|
|
246
|
-
def env_set(self, name:str, value:Any, wait: bool = True) ->
|
|
264
|
+
def env_set(self, name: str, value: Any, wait: bool = True) -> bool:
|
|
247
265
|
"""Set an environment variable."""
|
|
248
|
-
self._worker.env_set(name, value, wait=wait)
|
|
266
|
+
return self._worker.env_set(name, value, wait=wait)
|
|
249
267
|
|
|
250
|
-
def env_get(self, name:str=None, default:Any=None) -> dict|Any:
|
|
268
|
+
def env_get(self, name: str = None, default: Any = None) -> dict | Any:
|
|
251
269
|
"""Get an environment variable."""
|
|
252
270
|
if name is None:
|
|
253
|
-
return self._worker.env_get_all()
|
|
271
|
+
return self._worker.env_get_all()
|
|
254
272
|
|
|
255
|
-
|
|
256
|
-
return default if value is None else value
|
|
273
|
+
return self._worker.env_get(name, default=default)
|
|
257
274
|
|
|
258
|
-
def env_subscribe(self, name:str, callback:Callable) ->
|
|
275
|
+
def env_subscribe(self, name: str, callback: Callable) -> bool:
|
|
259
276
|
"""Subscribe to changes in an environment variable."""
|
|
260
|
-
self._worker.env_subscribe(name, callback)
|
|
277
|
+
return self._worker.env_subscribe(name, callback)
|
|
278
|
+
|
|
279
|
+
def env_unsubscribe(self, name: str) -> bool:
|
|
280
|
+
"""Unsubscribe from changes in an environment variable."""
|
|
281
|
+
return self._worker.env_unsubscribe(name)
|
|
261
282
|
|
|
262
283
|
def plugin_call(self, plugin_id: str, method_name: str, *args, **kwargs) -> Any:
|
|
263
284
|
"""Call a method provided by the manager or another plugin."""
|
|
264
285
|
return self._worker.plugin_call(plugin_id, method_name, *args, **kwargs)
|
|
265
286
|
|
|
287
|
+
def plugin_errors(self, plugin_id: Optional[str] = None) -> List[str]:
|
|
288
|
+
"""Get recorded error messages for this plugin or another plugin."""
|
|
289
|
+
return self._worker.plugin_errors(plugin_id)
|
|
290
|
+
|
|
266
291
|
def plugin_load(
|
|
267
292
|
self,
|
|
268
293
|
descriptor: PluginDescriptor,
|
|
@@ -21,11 +21,23 @@ import logging
|
|
|
21
21
|
from concurrent.futures import Future
|
|
22
22
|
from torch import multiprocessing as mp
|
|
23
23
|
from dataclasses import dataclass, field, fields
|
|
24
|
+
from enum import IntEnum
|
|
24
25
|
from multiprocessing.connection import Connection
|
|
25
26
|
|
|
26
27
|
import threading
|
|
27
28
|
|
|
28
29
|
|
|
30
|
+
class PluginStartupPhase(IntEnum):
|
|
31
|
+
"""Startup milestones reported by a plugin worker process."""
|
|
32
|
+
|
|
33
|
+
NOT_STARTED = 0
|
|
34
|
+
PROCESS_STARTED = 1
|
|
35
|
+
WAITING_FOR_DEBUGGER = 2
|
|
36
|
+
INITIALIZING = 3
|
|
37
|
+
READY = 4
|
|
38
|
+
FAILED = 5
|
|
39
|
+
|
|
40
|
+
|
|
29
41
|
|
|
30
42
|
if TYPE_CHECKING:
|
|
31
43
|
from parallelazy.plugin_rpc import PluginRPC
|
|
@@ -40,13 +52,38 @@ ResponseCallback = Callable[["PluginRPC","Response"], Any]
|
|
|
40
52
|
|
|
41
53
|
|
|
42
54
|
|
|
55
|
+
class _TopicShutdownSentinel:
|
|
56
|
+
"""Private sentinel used to stop topic listener threads cleanly."""
|
|
57
|
+
pass
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
_TOPIC_SHUTDOWN_SENTINEL = _TopicShutdownSentinel()
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
class _EnvMissing:
|
|
64
|
+
"""Sentinel indicating an environment key is not set (distinct from stored None)."""
|
|
65
|
+
pass
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
ENV_MISSING = _EnvMissing()
|
|
69
|
+
|
|
70
|
+
|
|
43
71
|
class QueueDescriptor:
|
|
44
|
-
"""Descriptor for holding
|
|
45
|
-
def __init__(
|
|
72
|
+
"""Descriptor for holding read/write connections representing a pipe-backed queue."""
|
|
73
|
+
def __init__(
|
|
74
|
+
self,
|
|
75
|
+
send: Optional[Connection] = None,
|
|
76
|
+
receive: Optional[Connection] = None,
|
|
77
|
+
*,
|
|
78
|
+
recieve: Optional[Connection] = None,
|
|
79
|
+
):
|
|
46
80
|
self.send = send
|
|
47
|
-
self.receive = recieve
|
|
81
|
+
self.receive = receive if receive is not None else recieve
|
|
48
82
|
|
|
49
|
-
def __str__(self):
|
|
83
|
+
def __str__(self):
|
|
84
|
+
send_closed = self.send.closed if self.send is not None else None
|
|
85
|
+
receive_closed = self.receive.closed if self.receive is not None else None
|
|
86
|
+
return f"QueueDescriptor(send_closed={send_closed}, receive_closed={receive_closed})"
|
|
50
87
|
|
|
51
88
|
|
|
52
89
|
|
|
@@ -58,11 +95,11 @@ class Queue:
|
|
|
58
95
|
def __init__(self, descriptor: QueueDescriptor = None):
|
|
59
96
|
if descriptor is None:
|
|
60
97
|
receive, send = mp.Pipe(duplex=False)
|
|
98
|
+
self._send = QueueSend(send)
|
|
99
|
+
self._receive = QueueReceive(receive)
|
|
61
100
|
else:
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
self._send = QueueSend(send)
|
|
65
|
-
self._receive = QueueRecive(receive)
|
|
101
|
+
self._send = QueueSend(descriptor.send) if descriptor.send is not None else None
|
|
102
|
+
self._receive = QueueReceive(descriptor.receive) if descriptor.receive is not None else None
|
|
66
103
|
|
|
67
104
|
@property
|
|
68
105
|
def sender(self):
|
|
@@ -73,27 +110,62 @@ class Queue:
|
|
|
73
110
|
return self._receive
|
|
74
111
|
|
|
75
112
|
def closed(self):
|
|
76
|
-
|
|
113
|
+
send_closed = self._send._conn.closed if self._send is not None else True
|
|
114
|
+
recv_closed = self._receive._conn.closed if self._receive is not None else True
|
|
115
|
+
if self._send is not None and self._receive is not None:
|
|
116
|
+
return send_closed or recv_closed
|
|
117
|
+
return send_closed and recv_closed
|
|
77
118
|
|
|
78
119
|
def put(self, item):
|
|
120
|
+
if self._send is None:
|
|
121
|
+
raise ValueError("Queue has no send end")
|
|
79
122
|
self._send.put(item)
|
|
80
123
|
|
|
81
124
|
def put_nowait(self, item):
|
|
82
|
-
self.
|
|
125
|
+
self.put(item)
|
|
126
|
+
|
|
127
|
+
def send_bytes(self, data: bytes):
|
|
128
|
+
if self._send is None:
|
|
129
|
+
raise ValueError("Queue has no send end")
|
|
130
|
+
self._send.send_bytes(data)
|
|
83
131
|
|
|
84
132
|
def get(self):
|
|
133
|
+
if self._receive is None:
|
|
134
|
+
raise ValueError("Queue has no receive end")
|
|
85
135
|
return self._receive.get()
|
|
86
136
|
|
|
87
137
|
def get_nowait(self):
|
|
88
|
-
return self.
|
|
138
|
+
return self.get()
|
|
139
|
+
|
|
140
|
+
def close_sender(self):
|
|
141
|
+
if self._send is not None:
|
|
142
|
+
try:
|
|
143
|
+
self._send.close()
|
|
144
|
+
except Exception:
|
|
145
|
+
pass
|
|
146
|
+
self._send = None
|
|
89
147
|
|
|
90
148
|
def close(self):
|
|
91
|
-
self.
|
|
92
|
-
self.
|
|
93
|
-
|
|
149
|
+
self.close_sender()
|
|
150
|
+
if self._receive is not None:
|
|
151
|
+
try:
|
|
152
|
+
self._receive.close()
|
|
153
|
+
except Exception:
|
|
154
|
+
pass
|
|
155
|
+
self._receive = None
|
|
94
156
|
|
|
95
157
|
def descriptor(self):
|
|
96
|
-
return QueueDescriptor(
|
|
158
|
+
return QueueDescriptor(
|
|
159
|
+
send=self._send._conn if self._send is not None else None,
|
|
160
|
+
receive=self._receive._conn if self._receive is not None else None,
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
def send_descriptor(self):
|
|
164
|
+
"""Return a descriptor containing only the write end of the pipe."""
|
|
165
|
+
return QueueDescriptor(
|
|
166
|
+
send=self._send._conn if self._send is not None else None,
|
|
167
|
+
receive=None,
|
|
168
|
+
)
|
|
97
169
|
|
|
98
170
|
|
|
99
171
|
class QueueSend:
|
|
@@ -105,11 +177,15 @@ class QueueSend:
|
|
|
105
177
|
with self._lock:
|
|
106
178
|
self._conn.send(item)
|
|
107
179
|
|
|
180
|
+
def send_bytes(self, data: bytes):
|
|
181
|
+
with self._lock:
|
|
182
|
+
self._conn.send_bytes(data)
|
|
183
|
+
|
|
108
184
|
def close(self):
|
|
109
185
|
self._conn.close()
|
|
110
186
|
|
|
111
187
|
|
|
112
|
-
class
|
|
188
|
+
class QueueReceive:
|
|
113
189
|
def __init__(self, conn: Connection):
|
|
114
190
|
self._conn = conn
|
|
115
191
|
|
|
@@ -119,6 +195,10 @@ class QueueRecive:
|
|
|
119
195
|
def close(self):
|
|
120
196
|
self._conn.close()
|
|
121
197
|
|
|
198
|
+
|
|
199
|
+
# Backwards-compatible alias for the legacy misspelled name.
|
|
200
|
+
QueueRecive = QueueReceive
|
|
201
|
+
|
|
122
202
|
class Channel:
|
|
123
203
|
"""
|
|
124
204
|
Duplex communication channel with request/response routing.
|
|
@@ -130,12 +210,12 @@ class Channel:
|
|
|
130
210
|
r1, w1 = mp.Pipe(duplex=False)
|
|
131
211
|
r2, w2 = mp.Pipe(duplex=False)
|
|
132
212
|
self._send = QueueSend(w1)
|
|
133
|
-
self._receive =
|
|
213
|
+
self._receive = QueueReceive(r2)
|
|
134
214
|
self._remote_send = QueueSend(w2)
|
|
135
|
-
self._remote_receive =
|
|
215
|
+
self._remote_receive = QueueReceive(r1)
|
|
136
216
|
else:
|
|
137
217
|
self._send = QueueSend(descriptor.send)
|
|
138
|
-
self._receive =
|
|
218
|
+
self._receive = QueueReceive(descriptor.receive)
|
|
139
219
|
self._remote_send = None
|
|
140
220
|
self._remote_receive = None
|
|
141
221
|
|
|
@@ -155,15 +235,21 @@ class Channel:
|
|
|
155
235
|
"""Receive a message, blocking until one arrives or the pipe closes."""
|
|
156
236
|
return self._receive.get()
|
|
157
237
|
|
|
238
|
+
def close_remote(self) -> None:
|
|
239
|
+
"""Close the remote pipe ends after handing them to the child process."""
|
|
240
|
+
if self._remote_send is not None:
|
|
241
|
+
self._remote_send.close()
|
|
242
|
+
self._remote_send = None
|
|
243
|
+
if self._remote_receive is not None:
|
|
244
|
+
self._remote_receive.close()
|
|
245
|
+
self._remote_receive = None
|
|
246
|
+
|
|
158
247
|
def shutdown(self):
|
|
159
248
|
if self._send:
|
|
160
249
|
self._send.close()
|
|
161
250
|
if self._receive:
|
|
162
251
|
self._receive.close()
|
|
163
|
-
|
|
164
|
-
self._remote_send.close()
|
|
165
|
-
if self._remote_receive:
|
|
166
|
-
self._remote_receive.close()
|
|
252
|
+
self.close_remote()
|
|
167
253
|
|
|
168
254
|
|
|
169
255
|
# ============================================================================
|
|
@@ -254,10 +340,16 @@ class Response(Message):
|
|
|
254
340
|
error: Optional[str] = None
|
|
255
341
|
|
|
256
342
|
class PendingRequest():
|
|
257
|
-
def __init__(
|
|
343
|
+
def __init__(
|
|
344
|
+
self,
|
|
345
|
+
message_id: str,
|
|
346
|
+
callback: Callable = None,
|
|
347
|
+
on_timeout: Optional[Callable[[str], None]] = None,
|
|
348
|
+
):
|
|
258
349
|
super().__init__()
|
|
259
350
|
self.future = Future()
|
|
260
351
|
self.message_id = message_id
|
|
352
|
+
self._on_timeout = on_timeout
|
|
261
353
|
if callback:
|
|
262
354
|
self.future.add_done_callback(callback)
|
|
263
355
|
|
|
@@ -267,16 +359,26 @@ class PendingRequest():
|
|
|
267
359
|
def set_result(self, result: Response|Any):
|
|
268
360
|
if not isinstance(result, Response):
|
|
269
361
|
result = Response(message_id=self.message_id, result=result)
|
|
270
|
-
self.future.
|
|
362
|
+
if not self.future.done():
|
|
363
|
+
try:
|
|
364
|
+
self.future.set_result(result)
|
|
365
|
+
except Exception:
|
|
366
|
+
pass
|
|
271
367
|
|
|
272
368
|
def set_error(self, error: str|Exception|Any):
|
|
273
|
-
self.future.
|
|
369
|
+
if not self.future.done():
|
|
370
|
+
try:
|
|
371
|
+
self.future.set_result(Response(message_id=self.message_id, error=str(error)))
|
|
372
|
+
except Exception:
|
|
373
|
+
pass
|
|
274
374
|
|
|
275
375
|
def wait(self, timeout=None) -> None | Response:
|
|
276
376
|
try:
|
|
277
377
|
result: Response = self.future.result(timeout=timeout)
|
|
278
378
|
return result
|
|
279
379
|
except TimeoutError:
|
|
380
|
+
if self._on_timeout is not None:
|
|
381
|
+
self._on_timeout(self.message_id)
|
|
280
382
|
return None
|
|
281
383
|
|
|
282
384
|
def __hash__(self):
|
|
@@ -292,6 +394,35 @@ class Subscription:
|
|
|
292
394
|
self.callback = callback
|
|
293
395
|
self.queue = queue or Queue() # For topic subscriptions, queue to receive data
|
|
294
396
|
self.thread: Optional[threading.Thread] = thread # Thread handling this subscription (if needed)
|
|
397
|
+
self._wakeup_r, self._wakeup_w = mp.Pipe(duplex=False)
|
|
398
|
+
self._wakeup_lock = threading.Lock()
|
|
399
|
+
self._stopped = False
|
|
400
|
+
|
|
401
|
+
def signal_stop(self) -> None:
|
|
402
|
+
"""Wake the listener thread without writing to the topic data pipe."""
|
|
403
|
+
with self._wakeup_lock:
|
|
404
|
+
if self._stopped:
|
|
405
|
+
return
|
|
406
|
+
self._stopped = True
|
|
407
|
+
try:
|
|
408
|
+
if not self._wakeup_w.closed:
|
|
409
|
+
self._wakeup_w.send_bytes(b"1")
|
|
410
|
+
except Exception:
|
|
411
|
+
pass
|
|
412
|
+
|
|
413
|
+
def close(self) -> None:
|
|
414
|
+
"""Signal stop and close both wakeup and topic pipe handles."""
|
|
415
|
+
self.signal_stop()
|
|
416
|
+
try:
|
|
417
|
+
self.queue.close()
|
|
418
|
+
except Exception:
|
|
419
|
+
pass
|
|
420
|
+
for conn in (self._wakeup_w, self._wakeup_r):
|
|
421
|
+
try:
|
|
422
|
+
if not conn.closed:
|
|
423
|
+
conn.close()
|
|
424
|
+
except Exception:
|
|
425
|
+
pass
|
|
295
426
|
|
|
296
427
|
class Subscriber:
|
|
297
428
|
"""Represents a subscriber to a topic or environment variable."""
|
|
@@ -310,6 +441,21 @@ class Subscriber:
|
|
|
310
441
|
@queue.setter
|
|
311
442
|
def queue(self, value: Queue):
|
|
312
443
|
self._queue = value
|
|
444
|
+
|
|
445
|
+
def close(self) -> None:
|
|
446
|
+
if self._queue is not None:
|
|
447
|
+
try:
|
|
448
|
+
self._queue.close()
|
|
449
|
+
except Exception:
|
|
450
|
+
pass
|
|
451
|
+
elif self.queue_descriptor is not None:
|
|
452
|
+
for conn in (self.queue_descriptor.send, self.queue_descriptor.receive):
|
|
453
|
+
if conn is not None:
|
|
454
|
+
try:
|
|
455
|
+
if not conn.closed:
|
|
456
|
+
conn.close()
|
|
457
|
+
except Exception:
|
|
458
|
+
pass
|
|
313
459
|
|
|
314
460
|
|
|
315
461
|
def __str__(self):
|
|
@@ -344,11 +490,12 @@ class PluginStatus:
|
|
|
344
490
|
|
|
345
491
|
@dataclass
|
|
346
492
|
class ProcessStatus:
|
|
347
|
-
"""
|
|
493
|
+
"""Manager-side process state, including the worker's startup phase."""
|
|
348
494
|
plugin_id: str
|
|
349
495
|
process_alive: bool
|
|
350
496
|
process_pid: Optional[int]
|
|
351
497
|
worker_status: Optional[PluginStatus] = None
|
|
498
|
+
startup_phase: Optional[str] = None
|
|
352
499
|
|
|
353
500
|
|
|
354
501
|
|
|
@@ -455,6 +602,7 @@ class RequestEnvUnsubscribe(Request):
|
|
|
455
602
|
class RequestEnvUpdate(Request):
|
|
456
603
|
command: CommandType = field(default=CommandType.ENV_UPDATE, init=False)
|
|
457
604
|
name: str = None
|
|
605
|
+
value: Any = None
|
|
458
606
|
|
|
459
607
|
### Pub/Sub Requests
|
|
460
608
|
|