py-sharedmemory 3.0.0__tar.gz → 3.0.1__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.
- {py_sharedmemory-3.0.0 → py_sharedmemory-3.0.1}/CHANGELOG.md +10 -0
- {py_sharedmemory-3.0.0 → py_sharedmemory-3.0.1}/LICENSE +21 -21
- py_sharedmemory-3.0.1/PKG-INFO +155 -0
- py_sharedmemory-3.0.0/py_sharedmemory.egg-info/PKG-INFO → py_sharedmemory-3.0.1/README.md +107 -98
- {py_sharedmemory-3.0.0 → py_sharedmemory-3.0.1}/benchmarks/benchmark.py +1 -4
- {py_sharedmemory-3.0.0 → py_sharedmemory-3.0.1}/benchmarks/plot.py +3 -3
- {py_sharedmemory-3.0.0 → py_sharedmemory-3.0.1}/benchmarks/results/windows-numpy.json +307 -307
- {py_sharedmemory-3.0.0 → py_sharedmemory-3.0.1}/memory/__init__.py +2 -1
- {py_sharedmemory-3.0.0 → py_sharedmemory-3.0.1}/memory/convert.py +16 -10
- {py_sharedmemory-3.0.0 → py_sharedmemory-3.0.1}/memory/factory.py +8 -0
- py_sharedmemory-3.0.1/memory/py.typed +0 -0
- {py_sharedmemory-3.0.0 → py_sharedmemory-3.0.1}/memory/receiver.py +10 -2
- {py_sharedmemory-3.0.0 → py_sharedmemory-3.0.1}/memory/sender.py +19 -8
- py_sharedmemory-3.0.1/py_sharedmemory.egg-info/PKG-INFO +155 -0
- {py_sharedmemory-3.0.0 → py_sharedmemory-3.0.1}/py_sharedmemory.egg-info/SOURCES.txt +2 -0
- {py_sharedmemory-3.0.0 → py_sharedmemory-3.0.1}/py_sharedmemory.egg-info/requires.txt +8 -0
- py_sharedmemory-3.0.1/pyproject.toml +100 -0
- {py_sharedmemory-3.0.0 → py_sharedmemory-3.0.1}/setup.cfg +4 -4
- {py_sharedmemory-3.0.0 → py_sharedmemory-3.0.1}/tests/test_benchmark.py +34 -0
- {py_sharedmemory-3.0.0 → py_sharedmemory-3.0.1}/tests/test_convert.py +31 -3
- py_sharedmemory-3.0.1/tests/test_package.py +34 -0
- {py_sharedmemory-3.0.0 → py_sharedmemory-3.0.1}/tests/test_processes.py +38 -0
- {py_sharedmemory-3.0.0 → py_sharedmemory-3.0.1}/tests/test_queue.py +64 -8
- py_sharedmemory-3.0.0/PKG-INFO +0 -98
- py_sharedmemory-3.0.0/README.md +0 -73
- py_sharedmemory-3.0.0/pyproject.toml +0 -54
- {py_sharedmemory-3.0.0 → py_sharedmemory-3.0.1}/MANIFEST.in +0 -0
- {py_sharedmemory-3.0.0 → py_sharedmemory-3.0.1}/benchmarks/__init__.py +0 -0
- {py_sharedmemory-3.0.0 → py_sharedmemory-3.0.1}/benchmarks/results/windows-numpy.png +0 -0
- {py_sharedmemory-3.0.0 → py_sharedmemory-3.0.1}/examples/basic.py +0 -0
- {py_sharedmemory-3.0.0 → py_sharedmemory-3.0.1}/py_sharedmemory.egg-info/dependency_links.txt +0 -0
- {py_sharedmemory-3.0.0 → py_sharedmemory-3.0.1}/py_sharedmemory.egg-info/top_level.txt +0 -0
- {py_sharedmemory-3.0.0 → py_sharedmemory-3.0.1}/tests/__init__.py +0 -0
- {py_sharedmemory-3.0.0 → py_sharedmemory-3.0.1}/tests/conftest.py +0 -0
- {py_sharedmemory-3.0.0 → py_sharedmemory-3.0.1}/tests/helpers.py +0 -0
- {py_sharedmemory-3.0.0 → py_sharedmemory-3.0.1}/tests/test_objects.py +0 -0
- {py_sharedmemory-3.0.0 → py_sharedmemory-3.0.1}/tests/test_threads.py +0 -0
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 3.0.1
|
|
4
|
+
|
|
5
|
+
- Fix resource-tracker leaks on Linux and macOS: receivers no longer register blocks they
|
|
6
|
+
attach to, so `fork` workers no longer accumulate tracker entries or print
|
|
7
|
+
"leaked shared_memory" warnings at exit.
|
|
8
|
+
- Ship type hints (`py.typed`), checked with strict mypy; expose `memory.__version__`.
|
|
9
|
+
- Support and test Python 3.15; test Linux, macOS, and Windows for every Python version.
|
|
10
|
+
- Require 100% branch coverage; add pre-commit hooks, Dependabot, and a trusted-publishing
|
|
11
|
+
release workflow.
|
|
12
|
+
|
|
3
13
|
## 3.0.0
|
|
4
14
|
|
|
5
15
|
- Python 3.11+, standard-library-only runtime.
|
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 Tobias Würth
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Tobias Würth
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: py-sharedmemory
|
|
3
|
+
Version: 3.0.1
|
|
4
|
+
Summary: Shared-memory queues for large Python objects with less pipe I/O
|
|
5
|
+
Author: Tobias Würth
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/tobiaswuerth/python_shared_memory_queue
|
|
8
|
+
Project-URL: Repository, https://github.com/tobiaswuerth/python_shared_memory_queue
|
|
9
|
+
Project-URL: Changelog, https://github.com/tobiaswuerth/python_shared_memory_queue/blob/main/CHANGELOG.md
|
|
10
|
+
Project-URL: Bug Tracker, https://github.com/tobiaswuerth/python_shared_memory_queue/issues
|
|
11
|
+
Keywords: queue,multiprocessing,shared-memory,inter-process-communication,ipc,numpy,zero-copy,pickle
|
|
12
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Intended Audience :: Science/Research
|
|
15
|
+
Classifier: Operating System :: MacOS
|
|
16
|
+
Classifier: Operating System :: Microsoft :: Windows
|
|
17
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
18
|
+
Classifier: Programming Language :: Python :: 3
|
|
19
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
24
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
25
|
+
Classifier: Topic :: Scientific/Engineering
|
|
26
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
27
|
+
Classifier: Topic :: System :: Distributed Computing
|
|
28
|
+
Classifier: Typing :: Typed
|
|
29
|
+
Requires-Python: >=3.11
|
|
30
|
+
Description-Content-Type: text/markdown
|
|
31
|
+
License-File: LICENSE
|
|
32
|
+
Provides-Extra: test
|
|
33
|
+
Requires-Dist: pytest>=7.4; extra == "test"
|
|
34
|
+
Requires-Dist: pytest-cov>=4.1; extra == "test"
|
|
35
|
+
Requires-Dist: pytest-timeout>=2.3; extra == "test"
|
|
36
|
+
Requires-Dist: numpy>=1.26; extra == "test"
|
|
37
|
+
Provides-Extra: benchmark
|
|
38
|
+
Requires-Dist: numpy>=1.26; extra == "benchmark"
|
|
39
|
+
Requires-Dist: matplotlib>=3.8; extra == "benchmark"
|
|
40
|
+
Provides-Extra: dev
|
|
41
|
+
Requires-Dist: py-sharedmemory[benchmark,test]; extra == "dev"
|
|
42
|
+
Requires-Dist: build>=1.2; extra == "dev"
|
|
43
|
+
Requires-Dist: mypy>=1.10; extra == "dev"
|
|
44
|
+
Requires-Dist: pre-commit>=3.7; extra == "dev"
|
|
45
|
+
Requires-Dist: ruff>=0.15; extra == "dev"
|
|
46
|
+
Requires-Dist: twine>=5; extra == "dev"
|
|
47
|
+
Dynamic: license-file
|
|
48
|
+
|
|
49
|
+
# py-sharedmemory
|
|
50
|
+
|
|
51
|
+
[](https://github.com/tobiaswuerth/python_shared_memory_queue/actions/workflows/ci.yml)
|
|
52
|
+
[](https://pypi.org/project/py-sharedmemory/)
|
|
53
|
+
[](https://pypi.org/project/py-sharedmemory/)
|
|
54
|
+
[](https://pypistats.org/packages/py-sharedmemory)
|
|
55
|
+
[](https://github.com/tobiaswuerth/python_shared_memory_queue/actions/workflows/ci.yml)
|
|
56
|
+
[](LICENSE)
|
|
57
|
+
[](https://github.com/astral-sh/ruff)
|
|
58
|
+
[](https://mypy-lang.org/)
|
|
59
|
+
[](https://github.com/pre-commit/pre-commit)
|
|
60
|
+
|
|
61
|
+
Send large Python objects between processes with less pipe I/O.
|
|
62
|
+
|
|
63
|
+
Originally built to reduce the heavy Windows pipe I/O and uneven throughput caused by
|
|
64
|
+
large `multiprocessing.Queue` transfers. Payloads travel through shared memory;
|
|
65
|
+
queues carry only small metadata, avoiding bulk transfers through the pipe.
|
|
66
|
+
|
|
67
|
+
Useful for **ML pipelines, reinforcement-learning actors and replay buffers,
|
|
68
|
+
scientific computing, and image/video processing**. Supports pickleable Python
|
|
69
|
+
objects with familiar `put/get` calls. No runtime dependencies, fully typed.
|
|
70
|
+
Python 3.11+ on Linux, macOS, and Windows.
|
|
71
|
+
|
|
72
|
+
```sh
|
|
73
|
+
pip install py-sharedmemory
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Measured performance
|
|
77
|
+
|
|
78
|
+

|
|
79
|
+
|
|
80
|
+
About **2× faster at 1–10 MB**; **10 GB transferred in 8.0 s**.
|
|
81
|
+
`mp.Queue` failed at 10 GB (Windows error 87). Small messages favor standard queues.
|
|
82
|
+
|
|
83
|
+
Core i9-14900K, 191.8 GiB RAM, Windows 11, Python 3.12.10, NumPy 2.5.3.
|
|
84
|
+
2026-10-08; contiguous float32 arrays; `spawn`; capacity 2; median of 5 runs,
|
|
85
|
+
excluding startup and warmup. Up to 1,000 messages or 512 MiB/run, at least one
|
|
86
|
+
message. GB = 10⁹ bytes. [Raw results](benchmarks/results/windows-numpy.json).
|
|
87
|
+
These measurements show transfer speed; I/O and stability depend on the workload.
|
|
88
|
+
|
|
89
|
+
## Usage
|
|
90
|
+
|
|
91
|
+
```python
|
|
92
|
+
import multiprocessing as mp
|
|
93
|
+
from memory import create_shared_memory_pair
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def produce(sender):
|
|
97
|
+
with sender:
|
|
98
|
+
sender.put({"step": 42, "frames": bytearray(16_000_000)})
|
|
99
|
+
sender.wait_for_all_ack()
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
if __name__ == "__main__":
|
|
103
|
+
ctx = mp.get_context("spawn")
|
|
104
|
+
sender, receiver = create_shared_memory_pair(capacity=2, ctx=ctx)
|
|
105
|
+
worker = ctx.Process(target=produce, args=(sender,))
|
|
106
|
+
worker.start()
|
|
107
|
+
with receiver:
|
|
108
|
+
batch = receiver.get(timeout=30)
|
|
109
|
+
worker.join()
|
|
110
|
+
sender.close()
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
One producing process per pair; multiple threads and consumers are supported.
|
|
114
|
+
Pass endpoints to workers before use. `put/get` support `block`, `timeout`, and
|
|
115
|
+
`*_nowait`. Capacity counts outstanding messages; 0/`None` is unbounded.
|
|
116
|
+
|
|
117
|
+
**Drain before producer close or exit:** `wait_for_all_ack(timeout=...)` returns
|
|
118
|
+
`False` on timeout. Close discards unread data. Within one process, drain before
|
|
119
|
+
closing either endpoint. Received data stays valid after cleanup; arrays preserve mutability.
|
|
120
|
+
|
|
121
|
+
A [complete example](examples/basic.py) includes error handling.
|
|
122
|
+
|
|
123
|
+
### API
|
|
124
|
+
|
|
125
|
+
| Call | Behavior |
|
|
126
|
+
| --- | --- |
|
|
127
|
+
| `create_shared_memory_pair(capacity=0, *, ctx=None)` | Returns `(sender, receiver)` built from one multiprocessing context. |
|
|
128
|
+
| `sender.put(obj, block=True, timeout=None)` / `put_nowait(obj)` | Copies `obj` into a new shared-memory block; raises `queue.Full`. |
|
|
129
|
+
| `sender.wait_for_all_ack(timeout=None)` | Waits until every block is received and freed; `False` on timeout. |
|
|
130
|
+
| `receiver.get(block=True, timeout=None)` / `get_nowait()` | Returns an independent local copy; raises `queue.Empty`. |
|
|
131
|
+
| `close()`, `closed`, `with endpoint:` | Both endpoints; using a closed endpoint raises `BrokenPipeError`. |
|
|
132
|
+
|
|
133
|
+
## Development
|
|
134
|
+
|
|
135
|
+
```sh
|
|
136
|
+
python -m pip install -e ".[dev]"
|
|
137
|
+
pre-commit install
|
|
138
|
+
python -m pytest --cov
|
|
139
|
+
pre-commit run --all-files
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
CI runs the tests on Linux, macOS, and Windows with Python 3.11–3.15 and requires
|
|
143
|
+
100% branch coverage on each. Pre-commit runs Ruff, mypy (strict), and file checks.
|
|
144
|
+
|
|
145
|
+
To release, update `__version__` in `memory/__init__.py` and the changelog, then
|
|
146
|
+
publish a GitHub release tagged `vX.Y.Z`. The release workflow uploads it to PyPI.
|
|
147
|
+
|
|
148
|
+
### Benchmark
|
|
149
|
+
|
|
150
|
+
```sh
|
|
151
|
+
python -m benchmarks.benchmark --sizes 16 100 1000 10000 100000 1000000 10000000 100000000 1000000000 10000000000 --repeats 5 --json benchmarks/results/windows-numpy.json
|
|
152
|
+
python -m benchmarks.plot benchmarks/results/windows-numpy.json benchmarks/results/windows-numpy.png
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
The full benchmark needs enough RAM for several payload copies.
|
|
@@ -1,98 +1,107 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
1
|
+
# py-sharedmemory
|
|
2
|
+
|
|
3
|
+
[](https://github.com/tobiaswuerth/python_shared_memory_queue/actions/workflows/ci.yml)
|
|
4
|
+
[](https://pypi.org/project/py-sharedmemory/)
|
|
5
|
+
[](https://pypi.org/project/py-sharedmemory/)
|
|
6
|
+
[](https://pypistats.org/packages/py-sharedmemory)
|
|
7
|
+
[](https://github.com/tobiaswuerth/python_shared_memory_queue/actions/workflows/ci.yml)
|
|
8
|
+
[](LICENSE)
|
|
9
|
+
[](https://github.com/astral-sh/ruff)
|
|
10
|
+
[](https://mypy-lang.org/)
|
|
11
|
+
[](https://github.com/pre-commit/pre-commit)
|
|
12
|
+
|
|
13
|
+
Send large Python objects between processes with less pipe I/O.
|
|
14
|
+
|
|
15
|
+
Originally built to reduce the heavy Windows pipe I/O and uneven throughput caused by
|
|
16
|
+
large `multiprocessing.Queue` transfers. Payloads travel through shared memory;
|
|
17
|
+
queues carry only small metadata, avoiding bulk transfers through the pipe.
|
|
18
|
+
|
|
19
|
+
Useful for **ML pipelines, reinforcement-learning actors and replay buffers,
|
|
20
|
+
scientific computing, and image/video processing**. Supports pickleable Python
|
|
21
|
+
objects with familiar `put/get` calls. No runtime dependencies, fully typed.
|
|
22
|
+
Python 3.11+ on Linux, macOS, and Windows.
|
|
23
|
+
|
|
24
|
+
```sh
|
|
25
|
+
pip install py-sharedmemory
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Measured performance
|
|
29
|
+
|
|
30
|
+

|
|
31
|
+
|
|
32
|
+
About **2× faster at 1–10 MB**; **10 GB transferred in 8.0 s**.
|
|
33
|
+
`mp.Queue` failed at 10 GB (Windows error 87). Small messages favor standard queues.
|
|
34
|
+
|
|
35
|
+
Core i9-14900K, 191.8 GiB RAM, Windows 11, Python 3.12.10, NumPy 2.5.3.
|
|
36
|
+
2026-10-08; contiguous float32 arrays; `spawn`; capacity 2; median of 5 runs,
|
|
37
|
+
excluding startup and warmup. Up to 1,000 messages or 512 MiB/run, at least one
|
|
38
|
+
message. GB = 10⁹ bytes. [Raw results](benchmarks/results/windows-numpy.json).
|
|
39
|
+
These measurements show transfer speed; I/O and stability depend on the workload.
|
|
40
|
+
|
|
41
|
+
## Usage
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
import multiprocessing as mp
|
|
45
|
+
from memory import create_shared_memory_pair
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def produce(sender):
|
|
49
|
+
with sender:
|
|
50
|
+
sender.put({"step": 42, "frames": bytearray(16_000_000)})
|
|
51
|
+
sender.wait_for_all_ack()
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
if __name__ == "__main__":
|
|
55
|
+
ctx = mp.get_context("spawn")
|
|
56
|
+
sender, receiver = create_shared_memory_pair(capacity=2, ctx=ctx)
|
|
57
|
+
worker = ctx.Process(target=produce, args=(sender,))
|
|
58
|
+
worker.start()
|
|
59
|
+
with receiver:
|
|
60
|
+
batch = receiver.get(timeout=30)
|
|
61
|
+
worker.join()
|
|
62
|
+
sender.close()
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
One producing process per pair; multiple threads and consumers are supported.
|
|
66
|
+
Pass endpoints to workers before use. `put/get` support `block`, `timeout`, and
|
|
67
|
+
`*_nowait`. Capacity counts outstanding messages; 0/`None` is unbounded.
|
|
68
|
+
|
|
69
|
+
**Drain before producer close or exit:** `wait_for_all_ack(timeout=...)` returns
|
|
70
|
+
`False` on timeout. Close discards unread data. Within one process, drain before
|
|
71
|
+
closing either endpoint. Received data stays valid after cleanup; arrays preserve mutability.
|
|
72
|
+
|
|
73
|
+
A [complete example](examples/basic.py) includes error handling.
|
|
74
|
+
|
|
75
|
+
### API
|
|
76
|
+
|
|
77
|
+
| Call | Behavior |
|
|
78
|
+
| --- | --- |
|
|
79
|
+
| `create_shared_memory_pair(capacity=0, *, ctx=None)` | Returns `(sender, receiver)` built from one multiprocessing context. |
|
|
80
|
+
| `sender.put(obj, block=True, timeout=None)` / `put_nowait(obj)` | Copies `obj` into a new shared-memory block; raises `queue.Full`. |
|
|
81
|
+
| `sender.wait_for_all_ack(timeout=None)` | Waits until every block is received and freed; `False` on timeout. |
|
|
82
|
+
| `receiver.get(block=True, timeout=None)` / `get_nowait()` | Returns an independent local copy; raises `queue.Empty`. |
|
|
83
|
+
| `close()`, `closed`, `with endpoint:` | Both endpoints; using a closed endpoint raises `BrokenPipeError`. |
|
|
84
|
+
|
|
85
|
+
## Development
|
|
86
|
+
|
|
87
|
+
```sh
|
|
88
|
+
python -m pip install -e ".[dev]"
|
|
89
|
+
pre-commit install
|
|
90
|
+
python -m pytest --cov
|
|
91
|
+
pre-commit run --all-files
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
CI runs the tests on Linux, macOS, and Windows with Python 3.11–3.15 and requires
|
|
95
|
+
100% branch coverage on each. Pre-commit runs Ruff, mypy (strict), and file checks.
|
|
96
|
+
|
|
97
|
+
To release, update `__version__` in `memory/__init__.py` and the changelog, then
|
|
98
|
+
publish a GitHub release tagged `vX.Y.Z`. The release workflow uploads it to PyPI.
|
|
99
|
+
|
|
100
|
+
### Benchmark
|
|
101
|
+
|
|
102
|
+
```sh
|
|
103
|
+
python -m benchmarks.benchmark --sizes 16 100 1000 10000 100000 1000000 10000000 100000000 1000000000 10000000000 --repeats 5 --json benchmarks/results/windows-numpy.json
|
|
104
|
+
python -m benchmarks.plot benchmarks/results/windows-numpy.json benchmarks/results/windows-numpy.png
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
The full benchmark needs enough RAM for several payload copies.
|
|
@@ -129,10 +129,7 @@ def main(argv=None):
|
|
|
129
129
|
print(f"{'Bytes':>12} {'Transport':>9} {'MiB/s':>10} {'ms/message':>12}")
|
|
130
130
|
for size in args.sizes:
|
|
131
131
|
count = args.messages or min(1_000, max(1, 2**29 // size))
|
|
132
|
-
if args.payload == "numpy"
|
|
133
|
-
payload = np.ones(size // 4, dtype=np.float32)
|
|
134
|
-
else:
|
|
135
|
-
payload = b"x" * size
|
|
132
|
+
payload = np.ones(size // 4, dtype=np.float32) if args.payload == "numpy" else b"x" * size
|
|
136
133
|
for transport in ("standard", "shared"):
|
|
137
134
|
row = {"transport": transport, "size": size, "messages": count}
|
|
138
135
|
durations = []
|
|
@@ -7,7 +7,7 @@ from pathlib import Path
|
|
|
7
7
|
import matplotlib
|
|
8
8
|
|
|
9
9
|
matplotlib.use("Agg")
|
|
10
|
-
import matplotlib.pyplot as plt
|
|
10
|
+
import matplotlib.pyplot as plt
|
|
11
11
|
|
|
12
12
|
|
|
13
13
|
def plot(report, output):
|
|
@@ -68,11 +68,11 @@ def _size_label(size):
|
|
|
68
68
|
return f"{size} B"
|
|
69
69
|
|
|
70
70
|
|
|
71
|
-
def main():
|
|
71
|
+
def main(argv=None):
|
|
72
72
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
73
73
|
parser.add_argument("results", type=Path)
|
|
74
74
|
parser.add_argument("output", type=Path)
|
|
75
|
-
args = parser.parse_args()
|
|
75
|
+
args = parser.parse_args(argv)
|
|
76
76
|
plot(json.loads(args.results.read_text(encoding="utf-8")), args.output)
|
|
77
77
|
|
|
78
78
|
|