parallelazy 0.1.3__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.3 → parallelazy-0.2.0}/PKG-INFO +17 -6
- {parallelazy-0.1.3 → parallelazy-0.2.0}/README.md +3 -2
- {parallelazy-0.1.3 → parallelazy-0.2.0}/parallelazy/__init__.py +3 -3
- {parallelazy-0.1.3 → parallelazy-0.2.0}/parallelazy/environment_manager.py +9 -5
- {parallelazy-0.1.3 → parallelazy-0.2.0}/parallelazy/plugin.py +41 -19
- {parallelazy-0.1.3 → parallelazy-0.2.0}/parallelazy/plugin_data.py +160 -26
- {parallelazy-0.1.3 → parallelazy-0.2.0}/parallelazy/plugin_logging.py +6 -4
- {parallelazy-0.1.3 → parallelazy-0.2.0}/parallelazy/plugin_manager.py +239 -98
- {parallelazy-0.1.3 → parallelazy-0.2.0}/parallelazy/plugin_monitor.py +14 -5
- {parallelazy-0.1.3 → parallelazy-0.2.0}/parallelazy/plugin_rpc.py +153 -53
- parallelazy-0.2.0/parallelazy/plugin_transport.py +350 -0
- {parallelazy-0.1.3 → parallelazy-0.2.0}/parallelazy/plugin_worker.py +581 -193
- {parallelazy-0.1.3 → parallelazy-0.2.0}/parallelazy/utils.py +5 -3
- {parallelazy-0.1.3 → parallelazy-0.2.0}/parallelazy.egg-info/PKG-INFO +17 -6
- {parallelazy-0.1.3 → parallelazy-0.2.0}/parallelazy.egg-info/SOURCES.txt +7 -1
- parallelazy-0.2.0/parallelazy.egg-info/requires.txt +6 -0
- {parallelazy-0.1.3 → parallelazy-0.2.0}/pyproject.toml +17 -4
- 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.3 → parallelazy-0.2.0}/tests/test_plugin_lifecycle.py +3 -1
- parallelazy-0.1.3/parallelazy/plugin_transport.py +0 -365
- parallelazy-0.1.3/parallelazy.egg-info/requires.txt +0 -5
- parallelazy-0.1.3/tests/test_env_cuda_ipc.py +0 -41
- {parallelazy-0.1.3 → parallelazy-0.2.0}/LICENSE +0 -0
- {parallelazy-0.1.3 → parallelazy-0.2.0}/parallelazy.egg-info/dependency_links.txt +0 -0
- {parallelazy-0.1.3 → parallelazy-0.2.0}/parallelazy.egg-info/top_level.txt +0 -0
- {parallelazy-0.1.3 → parallelazy-0.2.0}/setup.cfg +0 -0
- {parallelazy-0.1.3 → parallelazy-0.2.0}/tests/test_plugin_logging.py +0 -0
- {parallelazy-0.1.3 → parallelazy-0.2.0}/tests/test_plugin_monitoring.py +0 -0
|
@@ -1,18 +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
|
|
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
|
|
9
18
|
Requires-Python: >=3.11
|
|
10
19
|
Description-Content-Type: text/markdown
|
|
11
20
|
License-File: LICENSE
|
|
12
21
|
Requires-Dist: torch
|
|
13
22
|
Requires-Dist: numpy
|
|
14
|
-
Provides-Extra:
|
|
15
|
-
Requires-Dist:
|
|
23
|
+
Provides-Extra: monitoring
|
|
24
|
+
Requires-Dist: psutil; extra == "monitoring"
|
|
25
|
+
Requires-Dist: nvidia-ml-py; extra == "monitoring"
|
|
16
26
|
Dynamic: license-file
|
|
17
27
|
|
|
18
28
|
# parallelazy
|
|
@@ -108,6 +118,7 @@ The complete runnable examples are in
|
|
|
108
118
|
[examples/](examples/). For example:
|
|
109
119
|
|
|
110
120
|
```bash
|
|
111
|
-
python -m examples.
|
|
112
|
-
python -m examples.
|
|
121
|
+
python -m examples.example_quickstart
|
|
122
|
+
python -m examples.example_tensor_pipeline
|
|
123
|
+
python -m examples.example_dynamic_orchestrator
|
|
113
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
|
|
@@ -93,7 +93,7 @@ class PluginDescriptor:
|
|
|
93
93
|
|
|
94
94
|
class Plugin:
|
|
95
95
|
"""
|
|
96
|
-
Base class for all
|
|
96
|
+
Base class for all parallelazy plugins.
|
|
97
97
|
|
|
98
98
|
Plugins run in isolated processes and communicate via:
|
|
99
99
|
1. Control channel: Load/unload/configure/service commands
|
|
@@ -123,7 +123,9 @@ class Plugin:
|
|
|
123
123
|
key = plugin_name or cls.__name__
|
|
124
124
|
|
|
125
125
|
if key in cls._registry:
|
|
126
|
-
|
|
126
|
+
logging.getLogger(__name__).debug(
|
|
127
|
+
"Overwriting existing plugin registration for '%s'", key
|
|
128
|
+
)
|
|
127
129
|
|
|
128
130
|
cls._registry[key] = cls
|
|
129
131
|
|
|
@@ -149,6 +151,11 @@ class Plugin:
|
|
|
149
151
|
all_options = cls.default_options().copy()
|
|
150
152
|
all_options.update(options or {})
|
|
151
153
|
|
|
154
|
+
resolved_description = (
|
|
155
|
+
description
|
|
156
|
+
if description is not None
|
|
157
|
+
else (cls.__doc__.strip() if cls.__doc__ else "")
|
|
158
|
+
)
|
|
152
159
|
|
|
153
160
|
return PluginDescriptor(
|
|
154
161
|
version=version,
|
|
@@ -157,7 +164,7 @@ class Plugin:
|
|
|
157
164
|
name= camel_to_kebab(name or cls.__name__),
|
|
158
165
|
package=f"{cls.__module__}",
|
|
159
166
|
class_name=cls.__name__,
|
|
160
|
-
description=
|
|
167
|
+
description=resolved_description,
|
|
161
168
|
work_dir=work_dir,
|
|
162
169
|
log_dir=log_dir,
|
|
163
170
|
log_level=log_level,
|
|
@@ -238,34 +245,49 @@ class Plugin:
|
|
|
238
245
|
"""Request plugin shutdown; use wait_for_stop() in blocking main() work."""
|
|
239
246
|
self._worker.stop()
|
|
240
247
|
|
|
241
|
-
def topic_subscribe(self, topic:str, callback:Callable) ->
|
|
242
|
-
"""Subscribe to a data topic
|
|
243
|
-
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)
|
|
244
259
|
|
|
245
|
-
def
|
|
246
|
-
"""
|
|
247
|
-
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)
|
|
248
263
|
|
|
249
|
-
def env_set(self, name:str, value:Any, wait: bool = True) ->
|
|
264
|
+
def env_set(self, name: str, value: Any, wait: bool = True) -> bool:
|
|
250
265
|
"""Set an environment variable."""
|
|
251
|
-
self._worker.env_set(name, value, wait=wait)
|
|
266
|
+
return self._worker.env_set(name, value, wait=wait)
|
|
252
267
|
|
|
253
|
-
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:
|
|
254
269
|
"""Get an environment variable."""
|
|
255
270
|
if name is None:
|
|
256
|
-
return self._worker.env_get_all()
|
|
271
|
+
return self._worker.env_get_all()
|
|
257
272
|
|
|
258
|
-
|
|
259
|
-
return default if value is None else value
|
|
273
|
+
return self._worker.env_get(name, default=default)
|
|
260
274
|
|
|
261
|
-
def env_subscribe(self, name:str, callback:Callable) ->
|
|
275
|
+
def env_subscribe(self, name: str, callback: Callable) -> bool:
|
|
262
276
|
"""Subscribe to changes in an environment variable."""
|
|
263
|
-
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)
|
|
264
282
|
|
|
265
283
|
def plugin_call(self, plugin_id: str, method_name: str, *args, **kwargs) -> Any:
|
|
266
284
|
"""Call a method provided by the manager or another plugin."""
|
|
267
285
|
return self._worker.plugin_call(plugin_id, method_name, *args, **kwargs)
|
|
268
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
|
+
|
|
269
291
|
def plugin_load(
|
|
270
292
|
self,
|
|
271
293
|
descriptor: PluginDescriptor,
|
|
@@ -52,13 +52,38 @@ ResponseCallback = Callable[["PluginRPC","Response"], Any]
|
|
|
52
52
|
|
|
53
53
|
|
|
54
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
|
+
|
|
55
71
|
class QueueDescriptor:
|
|
56
|
-
"""Descriptor for holding
|
|
57
|
-
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
|
+
):
|
|
58
80
|
self.send = send
|
|
59
|
-
self.receive = recieve
|
|
81
|
+
self.receive = receive if receive is not None else recieve
|
|
60
82
|
|
|
61
|
-
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})"
|
|
62
87
|
|
|
63
88
|
|
|
64
89
|
|
|
@@ -70,11 +95,11 @@ class Queue:
|
|
|
70
95
|
def __init__(self, descriptor: QueueDescriptor = None):
|
|
71
96
|
if descriptor is None:
|
|
72
97
|
receive, send = mp.Pipe(duplex=False)
|
|
98
|
+
self._send = QueueSend(send)
|
|
99
|
+
self._receive = QueueReceive(receive)
|
|
73
100
|
else:
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
self._send = QueueSend(send)
|
|
77
|
-
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
|
|
78
103
|
|
|
79
104
|
@property
|
|
80
105
|
def sender(self):
|
|
@@ -85,27 +110,62 @@ class Queue:
|
|
|
85
110
|
return self._receive
|
|
86
111
|
|
|
87
112
|
def closed(self):
|
|
88
|
-
|
|
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
|
|
89
118
|
|
|
90
119
|
def put(self, item):
|
|
120
|
+
if self._send is None:
|
|
121
|
+
raise ValueError("Queue has no send end")
|
|
91
122
|
self._send.put(item)
|
|
92
123
|
|
|
93
124
|
def put_nowait(self, item):
|
|
94
|
-
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)
|
|
95
131
|
|
|
96
132
|
def get(self):
|
|
133
|
+
if self._receive is None:
|
|
134
|
+
raise ValueError("Queue has no receive end")
|
|
97
135
|
return self._receive.get()
|
|
98
136
|
|
|
99
137
|
def get_nowait(self):
|
|
100
|
-
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
|
|
101
147
|
|
|
102
148
|
def close(self):
|
|
103
|
-
self.
|
|
104
|
-
self.
|
|
105
|
-
|
|
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
|
|
106
156
|
|
|
107
157
|
def descriptor(self):
|
|
108
|
-
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
|
+
)
|
|
109
169
|
|
|
110
170
|
|
|
111
171
|
class QueueSend:
|
|
@@ -117,11 +177,15 @@ class QueueSend:
|
|
|
117
177
|
with self._lock:
|
|
118
178
|
self._conn.send(item)
|
|
119
179
|
|
|
180
|
+
def send_bytes(self, data: bytes):
|
|
181
|
+
with self._lock:
|
|
182
|
+
self._conn.send_bytes(data)
|
|
183
|
+
|
|
120
184
|
def close(self):
|
|
121
185
|
self._conn.close()
|
|
122
186
|
|
|
123
187
|
|
|
124
|
-
class
|
|
188
|
+
class QueueReceive:
|
|
125
189
|
def __init__(self, conn: Connection):
|
|
126
190
|
self._conn = conn
|
|
127
191
|
|
|
@@ -131,6 +195,10 @@ class QueueRecive:
|
|
|
131
195
|
def close(self):
|
|
132
196
|
self._conn.close()
|
|
133
197
|
|
|
198
|
+
|
|
199
|
+
# Backwards-compatible alias for the legacy misspelled name.
|
|
200
|
+
QueueRecive = QueueReceive
|
|
201
|
+
|
|
134
202
|
class Channel:
|
|
135
203
|
"""
|
|
136
204
|
Duplex communication channel with request/response routing.
|
|
@@ -142,12 +210,12 @@ class Channel:
|
|
|
142
210
|
r1, w1 = mp.Pipe(duplex=False)
|
|
143
211
|
r2, w2 = mp.Pipe(duplex=False)
|
|
144
212
|
self._send = QueueSend(w1)
|
|
145
|
-
self._receive =
|
|
213
|
+
self._receive = QueueReceive(r2)
|
|
146
214
|
self._remote_send = QueueSend(w2)
|
|
147
|
-
self._remote_receive =
|
|
215
|
+
self._remote_receive = QueueReceive(r1)
|
|
148
216
|
else:
|
|
149
217
|
self._send = QueueSend(descriptor.send)
|
|
150
|
-
self._receive =
|
|
218
|
+
self._receive = QueueReceive(descriptor.receive)
|
|
151
219
|
self._remote_send = None
|
|
152
220
|
self._remote_receive = None
|
|
153
221
|
|
|
@@ -167,15 +235,21 @@ class Channel:
|
|
|
167
235
|
"""Receive a message, blocking until one arrives or the pipe closes."""
|
|
168
236
|
return self._receive.get()
|
|
169
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
|
+
|
|
170
247
|
def shutdown(self):
|
|
171
248
|
if self._send:
|
|
172
249
|
self._send.close()
|
|
173
250
|
if self._receive:
|
|
174
251
|
self._receive.close()
|
|
175
|
-
|
|
176
|
-
self._remote_send.close()
|
|
177
|
-
if self._remote_receive:
|
|
178
|
-
self._remote_receive.close()
|
|
252
|
+
self.close_remote()
|
|
179
253
|
|
|
180
254
|
|
|
181
255
|
# ============================================================================
|
|
@@ -266,10 +340,16 @@ class Response(Message):
|
|
|
266
340
|
error: Optional[str] = None
|
|
267
341
|
|
|
268
342
|
class PendingRequest():
|
|
269
|
-
def __init__(
|
|
343
|
+
def __init__(
|
|
344
|
+
self,
|
|
345
|
+
message_id: str,
|
|
346
|
+
callback: Callable = None,
|
|
347
|
+
on_timeout: Optional[Callable[[str], None]] = None,
|
|
348
|
+
):
|
|
270
349
|
super().__init__()
|
|
271
350
|
self.future = Future()
|
|
272
351
|
self.message_id = message_id
|
|
352
|
+
self._on_timeout = on_timeout
|
|
273
353
|
if callback:
|
|
274
354
|
self.future.add_done_callback(callback)
|
|
275
355
|
|
|
@@ -279,16 +359,26 @@ class PendingRequest():
|
|
|
279
359
|
def set_result(self, result: Response|Any):
|
|
280
360
|
if not isinstance(result, Response):
|
|
281
361
|
result = Response(message_id=self.message_id, result=result)
|
|
282
|
-
self.future.
|
|
362
|
+
if not self.future.done():
|
|
363
|
+
try:
|
|
364
|
+
self.future.set_result(result)
|
|
365
|
+
except Exception:
|
|
366
|
+
pass
|
|
283
367
|
|
|
284
368
|
def set_error(self, error: str|Exception|Any):
|
|
285
|
-
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
|
|
286
374
|
|
|
287
375
|
def wait(self, timeout=None) -> None | Response:
|
|
288
376
|
try:
|
|
289
377
|
result: Response = self.future.result(timeout=timeout)
|
|
290
378
|
return result
|
|
291
379
|
except TimeoutError:
|
|
380
|
+
if self._on_timeout is not None:
|
|
381
|
+
self._on_timeout(self.message_id)
|
|
292
382
|
return None
|
|
293
383
|
|
|
294
384
|
def __hash__(self):
|
|
@@ -304,6 +394,35 @@ class Subscription:
|
|
|
304
394
|
self.callback = callback
|
|
305
395
|
self.queue = queue or Queue() # For topic subscriptions, queue to receive data
|
|
306
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
|
|
307
426
|
|
|
308
427
|
class Subscriber:
|
|
309
428
|
"""Represents a subscriber to a topic or environment variable."""
|
|
@@ -322,6 +441,21 @@ class Subscriber:
|
|
|
322
441
|
@queue.setter
|
|
323
442
|
def queue(self, value: Queue):
|
|
324
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
|
|
325
459
|
|
|
326
460
|
|
|
327
461
|
def __str__(self):
|
|
@@ -26,6 +26,7 @@ class PluginRuntimeSettings:
|
|
|
26
26
|
log_level: int
|
|
27
27
|
display_logs: bool
|
|
28
28
|
write_logs: bool = True
|
|
29
|
+
timeout_request: float = 5.0
|
|
29
30
|
|
|
30
31
|
|
|
31
32
|
def resolve_directory(path: str, base_dir: Optional[str] = None) -> str:
|
|
@@ -50,16 +51,17 @@ def plugin_log_filename(plugin_id: str) -> str:
|
|
|
50
51
|
return f"{readable_id}-{suffix}.log"
|
|
51
52
|
|
|
52
53
|
|
|
53
|
-
def default_runtime_settings() -> PluginRuntimeSettings:
|
|
54
|
+
def default_runtime_settings(write_logs: bool = True) -> PluginRuntimeSettings:
|
|
54
55
|
work_dir = resolve_directory(os.getcwd())
|
|
55
56
|
log_dir = os.path.join(resolve_directory("logs", work_dir), create_run_id())
|
|
56
|
-
|
|
57
|
+
if write_logs:
|
|
58
|
+
os.makedirs(log_dir, exist_ok=True)
|
|
57
59
|
return PluginRuntimeSettings(
|
|
58
60
|
work_dir=work_dir,
|
|
59
61
|
log_dir=log_dir,
|
|
60
62
|
log_level=logging.INFO,
|
|
61
63
|
display_logs=True,
|
|
62
|
-
write_logs=
|
|
64
|
+
write_logs=write_logs,
|
|
63
65
|
)
|
|
64
66
|
|
|
65
67
|
|
|
@@ -233,7 +235,7 @@ def _utc_timestamp() -> str:
|
|
|
233
235
|
class _RecoveringFileHandler(logging.Handler):
|
|
234
236
|
"""Append to a log file and recreate it if its path or parent is removed."""
|
|
235
237
|
|
|
236
|
-
terminator =
|
|
238
|
+
terminator = "\n"
|
|
237
239
|
|
|
238
240
|
def __init__(self, filename: str):
|
|
239
241
|
super().__init__()
|