Skip to content

Commit 0d3d06f

Browse files
committed
Adopt simpler SharedMemory API, collapsing PosixSharedMemory and WindowsNamedSharedMemory into one.
1 parent 6878533 commit 0d3d06f

File tree

3 files changed

+189
-246
lines changed

3 files changed

+189
-246
lines changed

Doc/library/multiprocessing.shared_memory.rst

Lines changed: 29 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -35,41 +35,33 @@ or other communications requiring the serialization/deserialization and
3535
copying of data.
3636

3737

38-
.. class:: SharedMemory(name, flags=None, mode=0o600, size=0, read_only=False)
38+
.. class:: SharedMemory(name=None, create=False, size=0)
3939

40-
This class creates and returns an instance of either a
41-
:class:`PosixSharedMemory` or :class:`NamedSharedMemory` class depending
42-
upon their availability on the local system.
40+
Creates a new shared memory block or attaches to an existing shared
41+
memory block. Each shared memory block is assigned a unique name.
42+
In this way, one process can create a shared memory block with a
43+
particular name and a different process can attach to that same shared
44+
memory block using that same name.
45+
46+
As a resource for sharing data across processes, shared memory blocks
47+
may outlive the original process that created them. When one process
48+
no longer needs access to a shared memory block that might still be
49+
needed by other processes, the :meth:`close()` method should be called.
50+
When a shared memory block is no longer needed by any process, the
51+
:meth:`unlink()` method should be called to ensure proper cleanup.
4352

4453
*name* is the unique name for the requested shared memory, specified as
45-
a string. If ``None`` is supplied for the name, a new shared memory
46-
block with a novel name will be created without needing to also specify
47-
``flags``.
48-
49-
*flags* is set to ``None`` when attempting to attach to an existing shared
50-
memory block by its unique name but if no existing block has that name, an
51-
exception will be raised. To request the creation of a new shared
52-
memory block, set to ``O_CREX``. To request the optional creation of a
53-
new shared memory block or attach to an existing one by the same name,
54-
set to ``O_CREAT``.
55-
56-
*mode* controls user/group/all-based read/write permissions on the
57-
shared memory block. Its specification is not enforceable on all platforms.
58-
59-
*size* specifies the number of bytes requested for a shared memory block.
60-
Because some platforms choose to allocate chunks of memory based upon
61-
that platform's memory page size, the exact size of the shared memory
62-
block may be larger or equal to the size requested. When attaching to an
63-
existing shared memory block, set to ``0`` (which is the default).
64-
Requesting a size greater than the original when attaching to an existing
65-
shared memory block will attempt a resize of the shared memory block
66-
which may or may not be successful. Requesting a size smaller than the
67-
original will attempt to attach to the first N bytes of the existing
68-
shared memory block but may still give access to the full allocated size.
69-
70-
*read_only* controls whether a shared memory block is to be available
71-
for only reading or for both reading and writing. Its specification is
72-
not enforceable on all platforms.
54+
a string. When creating a new shared memory block, if ``None`` (the
55+
default) is supplied for the name, a novel name will be generated.
56+
57+
*create* controls whether a new shared memory block is created (``True``)
58+
or an existing shared memory block is attached (``False``).
59+
60+
*size* specifies the requested number of bytes when creating a new shared
61+
memory block. Because some platforms choose to allocate chunks of memory
62+
based upon that platform's memory page size, the exact size of the shared
63+
memory block may be larger or equal to the size requested. When attaching
64+
to an existing shared memory block, the ``size`` parameter is ignored.
7365

7466
.. method:: close()
7567

@@ -100,10 +92,6 @@ copying of data.
10092

10193
Read-only access to the unique name of the shared memory block.
10294

103-
.. attribute:: mode
104-
105-
Read-only access to access permissions mode of the shared memory block.
106-
10795
.. attribute:: size
10896

10997
Read-only access to size in bytes of the shared memory block.
@@ -113,7 +101,7 @@ The following example demonstrates low-level use of :class:`SharedMemory`
113101
instances::
114102

115103
>>> from multiprocessing import shared_memory
116-
>>> shm_a = shared_memory.SharedMemory(None, size=10)
104+
>>> shm_a = shared_memory.SharedMemory(create=True, size=10)
117105
>>> type(shm_a.buf)
118106
<class 'memoryview'>
119107
>>> buffer = shm_a.buf
@@ -146,7 +134,7 @@ same ``numpy.ndarray`` from two distinct Python shells:
146134
>>> import numpy as np
147135
>>> a = np.array([1, 1, 2, 3, 5, 8]) # Start with an existing NumPy array
148136
>>> from multiprocessing import shared_memory
149-
>>> shm = shared_memory.SharedMemory(None, size=a.nbytes)
137+
>>> shm = shared_memory.SharedMemory(create=True, size=a.nbytes)
150138
>>> # Now create a NumPy array backed by shared memory
151139
>>> b = np.ndarray(a.shape, dtype=a.dtype, buffer=shm.buf)
152140
>>> b[:] = a[:] # Copy the original data into shared memory
@@ -163,7 +151,7 @@ same ``numpy.ndarray`` from two distinct Python shells:
163151
>>> import numpy as np
164152
>>> from multiprocessing import shared_memory
165153
>>> # Attach to the existing shared memory block
166-
>>> existing_shm = shared_memory.SharedMemory('psm_21467_46075')
154+
>>> existing_shm = shared_memory.SharedMemory(name='psm_21467_46075')
167155
>>> # Note that a.shape is (6,) and a.dtype is np.int64 in this example
168156
>>> c = np.ndarray((6,), dtype=np.int64, buffer=existing_shm.buf)
169157
>>> c
@@ -252,8 +240,8 @@ needed:
252240
>>> with shared_memory.SharedMemoryManager() as smm:
253241
... sl = smm.ShareableList(range(2000))
254242
... # Divide the work among two processes, storing partial results in sl
255-
... p1 = Process(target=do_work, args=(sl.shm.name, 0, 1000))
256-
... p2 = Process(target=do_work, args=(sl.shm.name, 1000, 2000))
243+
... p1 = Process(target=do_work, args=(sl, 0, 1000))
244+
... p2 = Process(target=do_work, args=(sl, 1000, 2000))
257245
... p1.start()
258246
... p2.start() # A multiprocessing.Pool might be more efficient
259247
... p1.join()

0 commit comments

Comments
 (0)