@@ -20,8 +20,8 @@ and management of shared memory to be accessed by one or more processes
20
20
on a multicore or symmetric multiprocessor (SMP) machine. To assist with
21
21
the life-cycle management of shared memory especially across distinct
22
22
processes, a :class: `~multiprocessing.managers.BaseManager ` subclass,
23
- :class: `SharedMemoryManager `, is also provided in the
24
- `` multiprocessing.managers ` ` module.
23
+ :class: `~multiprocessing.managers. SharedMemoryManager `, is also provided in the
24
+ :mod: ` multiprocessing.managers ` module.
25
25
26
26
In this module, shared memory refers to "System V style" shared memory blocks
27
27
(though is not necessarily implemented explicitly as such) and does not refer
@@ -38,7 +38,8 @@ copying of data.
38
38
39
39
.. class :: SharedMemory(name=None, create=False, size=0)
40
40
41
- Creates a new shared memory block or attaches to an existing shared
41
+ Create an instance of the :class: `!SharedMemory ` class for either
42
+ creating a new shared memory block or attaching to an existing shared
42
43
memory block. Each shared memory block is assigned a unique name.
43
44
In this way, one process can create a shared memory block with a
44
45
particular name and a different process can attach to that same shared
@@ -47,43 +48,48 @@ copying of data.
47
48
As a resource for sharing data across processes, shared memory blocks
48
49
may outlive the original process that created them. When one process
49
50
no longer needs access to a shared memory block that might still be
50
- needed by other processes, the :meth: `close() ` method should be called.
51
+ needed by other processes, the :meth: `close ` method should be called.
51
52
When a shared memory block is no longer needed by any process, the
52
- :meth: `unlink() ` method should be called to ensure proper cleanup.
53
-
54
- *name * is the unique name for the requested shared memory, specified as
55
- a string. When creating a new shared memory block, if ``None `` (the
56
- default) is supplied for the name, a novel name will be generated.
57
-
58
- *create * controls whether a new shared memory block is created (``True ``)
59
- or an existing shared memory block is attached (``False ``).
60
-
61
- *size * specifies the requested number of bytes when creating a new shared
62
- memory block. Because some platforms choose to allocate chunks of memory
63
- based upon that platform's memory page size, the exact size of the shared
64
- memory block may be larger or equal to the size requested. When attaching
65
- to an existing shared memory block, the ``size `` parameter is ignored.
53
+ :meth: `unlink ` method should be called to ensure proper cleanup.
54
+
55
+ :param name:
56
+ The unique name for the requested shared memory, specified as a string.
57
+ When creating a new shared memory block, if ``None `` (the default)
58
+ is supplied for the name, a novel name will be generated.
59
+ :type name: str | None
60
+
61
+ :param bool create:
62
+ Control whether a new shared memory block is created (``True ``)
63
+ or an existing shared memory block is attached (``False ``).
64
+
65
+ :param int size:
66
+ The requested number of bytes when creating a new shared memory block.
67
+ Because some platforms choose to allocate chunks of memory
68
+ based upon that platform's memory page size, the exact size of the shared
69
+ memory block may be larger or equal to the size requested.
70
+ When attaching to an existing shared memory block,
71
+ the *size * parameter is ignored.
66
72
67
73
.. method :: close()
68
74
69
- Closes access to the shared memory from this instance. In order to
75
+ Close access to the shared memory from this instance. In order to
70
76
ensure proper cleanup of resources, all instances should call
71
- `` close() ` ` once the instance is no longer needed. Note that calling
72
- `` close() ` ` does not cause the shared memory block itself to be
77
+ :meth: ` close ` once the instance is no longer needed. Note that calling
78
+ :meth: ` ! close ` does not cause the shared memory block itself to be
73
79
destroyed.
74
80
75
81
.. method :: unlink()
76
82
77
- Requests that the underlying shared memory block be destroyed. In
78
- order to ensure proper cleanup of resources, `` unlink() ` ` should be
83
+ Request that the underlying shared memory block be destroyed. In
84
+ order to ensure proper cleanup of resources, :meth: ` unlink ` should be
79
85
called once (and only once) across all processes which have need
80
86
for the shared memory block. After requesting its destruction, a
81
87
shared memory block may or may not be immediately destroyed and
82
88
this behavior may differ across platforms. Attempts to access data
83
- inside the shared memory block after `` unlink() ` ` has been called may
89
+ inside the shared memory block after :meth: ` ! unlink ` has been called may
84
90
result in memory access errors. Note: the last process relinquishing
85
- its hold on a shared memory block may call `` unlink() ` ` and
86
- :meth: `close() ` in either order.
91
+ its hold on a shared memory block may call :meth: ` ! unlink ` and
92
+ :meth: `close ` in either order.
87
93
88
94
.. attribute :: buf
89
95
@@ -126,7 +132,7 @@ instances::
126
132
127
133
The following example demonstrates a practical use of the :class: `SharedMemory `
128
134
class with `NumPy arrays <https://numpy.org/ >`_, accessing the
129
- same `` numpy.ndarray ` ` from two distinct Python shells:
135
+ same :class: ` ! numpy.ndarray ` from two distinct Python shells:
130
136
131
137
.. doctest ::
132
138
:options: +SKIP
@@ -178,43 +184,43 @@ same ``numpy.ndarray`` from two distinct Python shells:
178
184
.. class :: SharedMemoryManager([address[, authkey]])
179
185
:module: multiprocessing.managers
180
186
181
- A subclass of :class: `~ multiprocessing.managers.BaseManager ` which can be
187
+ A subclass of :class: `multiprocessing.managers.BaseManager ` which can be
182
188
used for the management of shared memory blocks across processes.
183
189
184
190
A call to :meth: `~multiprocessing.managers.BaseManager.start ` on a
185
- :class: `SharedMemoryManager ` instance causes a new process to be started.
191
+ :class: `! SharedMemoryManager ` instance causes a new process to be started.
186
192
This new process's sole purpose is to manage the life cycle
187
193
of all shared memory blocks created through it. To trigger the release
188
194
of all shared memory blocks managed by that process, call
189
- :meth: `~multiprocessing.managers.BaseManager.shutdown() ` on the instance.
190
- This triggers a :meth: `SharedMemory.unlink() ` call on all of the
191
- :class: `SharedMemory ` objects managed by that process and then
192
- stops the process itself. By creating `` SharedMemory ` ` instances
193
- through a `` SharedMemoryManager ` `, we avoid the need to manually track
195
+ :meth: `~multiprocessing.managers.BaseManager.shutdown ` on the instance.
196
+ This triggers a :meth: `~multiprocessing.shared_memory. SharedMemory.unlink ` call
197
+ on all of the :class: `SharedMemory ` objects managed by that process and then
198
+ stops the process itself. By creating :class: ` ! SharedMemory ` instances
199
+ through a :class: ` ! SharedMemoryManager `, we avoid the need to manually track
194
200
and trigger the freeing of shared memory resources.
195
201
196
202
This class provides methods for creating and returning :class: `SharedMemory `
197
203
instances and for creating a list-like object (:class: `ShareableList `)
198
204
backed by shared memory.
199
205
200
- Refer to :class: `multiprocessing.managers.BaseManager ` for a description
206
+ Refer to :class: `~ multiprocessing.managers.BaseManager ` for a description
201
207
of the inherited *address * and *authkey * optional input arguments and how
202
- they may be used to connect to an existing `` SharedMemoryManager ` ` service
208
+ they may be used to connect to an existing :class: ` ! SharedMemoryManager ` service
203
209
from other processes.
204
210
205
211
.. method :: SharedMemory(size)
206
212
207
213
Create and return a new :class: `SharedMemory ` object with the
208
- specified `` size `` in bytes.
214
+ specified * size * in bytes.
209
215
210
216
.. method :: ShareableList(sequence)
211
217
212
218
Create and return a new :class: `ShareableList ` object, initialized
213
- by the values from the input `` sequence `` .
219
+ by the values from the input * sequence * .
214
220
215
221
216
222
The following example demonstrates the basic mechanisms of a
217
- :class: `SharedMemoryManager `:
223
+ :class: `~multiprocessing.managers. SharedMemoryManager `:
218
224
219
225
.. doctest ::
220
226
:options: +SKIP
@@ -232,9 +238,9 @@ The following example demonstrates the basic mechanisms of a
232
238
>>> smm.shutdown() # Calls unlink() on sl, raw_shm, and another_sl
233
239
234
240
The following example depicts a potentially more convenient pattern for using
235
- :class: `SharedMemoryManager ` objects via the :keyword: ` with ` statement to
236
- ensure that all shared memory blocks are released after they are no longer
237
- needed:
241
+ :class: `~multiprocessing.managers. SharedMemoryManager ` objects via the
242
+ :keyword: ` with ` statement to ensure that all shared memory blocks are released
243
+ after they are no longer needed:
238
244
239
245
.. doctest ::
240
246
:options: +SKIP
@@ -250,38 +256,46 @@ needed:
250
256
... p2.join() # Wait for all work to complete in both processes
251
257
... total_result = sum (sl) # Consolidate the partial results now in sl
252
258
253
- When using a :class: `SharedMemoryManager ` in a :keyword: `with ` statement, the
254
- shared memory blocks created using that manager are all released when the
255
- :keyword: `with ` statement's code block finishes execution.
259
+ When using a :class: `~multiprocessing.managers.SharedMemoryManager `
260
+ in a :keyword: `with ` statement, the shared memory blocks created using that
261
+ manager are all released when the :keyword: `!with ` statement's code block
262
+ finishes execution.
263
+
264
+
265
+ .. class :: ShareableList(sequence=None, *, name=None)
256
266
267
+ Provide a mutable list-like object where all values stored within are
268
+ stored in a shared memory block.
269
+ This constrains storable values to the following built-in data types:
257
270
258
- .. class :: ShareableList(sequence=None, \*, name=None)
271
+ * :class: `int ` (signed 64-bit)
272
+ * :class: `float `
273
+ * :class: `bool `
274
+ * :class: `str ` (less than 10M bytes each when encoded as UTF-8)
275
+ * :class: `bytes ` (less than 10M bytes each)
276
+ * ``None ``
259
277
260
- Provides a mutable list-like object where all values stored within are
261
- stored in a shared memory block. This constrains storable values to
262
- only the ``int `` (signed 64-bit), ``float ``, ``bool ``, ``str `` (less
263
- than 10M bytes each when encoded as utf-8), ``bytes `` (less than 10M
264
- bytes each), and ``None `` built-in data types. It also notably
265
- differs from the built-in ``list `` type in that these lists can not
266
- change their overall length (i.e. no append, insert, etc.) and do not
267
- support the dynamic creation of new :class: `ShareableList ` instances
278
+ It also notably differs from the built-in :class: `list ` type
279
+ in that these lists can not change their overall length
280
+ (i.e. no :meth: `!append `, :meth: `!insert `, etc.) and do not
281
+ support the dynamic creation of new :class: `!ShareableList ` instances
268
282
via slicing.
269
283
270
- *sequence * is used in populating a new `` ShareableList ` ` full of values.
284
+ *sequence * is used in populating a new :class: ` ! ShareableList ` full of values.
271
285
Set to ``None `` to instead attach to an already existing
272
- `` ShareableList ` ` by its unique shared memory name.
286
+ :class: ` ! ShareableList ` by its unique shared memory name.
273
287
274
288
*name * is the unique name for the requested shared memory, as described
275
289
in the definition for :class: `SharedMemory `. When attaching to an
276
- existing `` ShareableList ` `, specify its shared memory block's unique
277
- name while leaving `` sequence `` set to ``None ``.
290
+ existing :class: ` ! ShareableList `, specify its shared memory block's unique
291
+ name while leaving * sequence * set to ``None ``.
278
292
279
293
.. note ::
280
294
281
295
A known issue exists for :class: `bytes ` and :class: `str ` values.
282
296
If they end with ``\x00 `` nul bytes or characters, those may be
283
297
*silently stripped * when fetching them by index from the
284
- :class: `ShareableList `. This ``.rstrip(b'\x00') `` behavior is
298
+ :class: `! ShareableList `. This ``.rstrip(b'\x00') `` behavior is
285
299
considered a bug and may go away in the future. See :gh: `106939 `.
286
300
287
301
For applications where rstripping of trailing nulls is a problem,
@@ -307,12 +321,12 @@ shared memory blocks created using that manager are all released when the
307
321
308
322
.. method :: count(value)
309
323
310
- Returns the number of occurrences of `` value `` .
324
+ Return the number of occurrences of * value * .
311
325
312
326
.. method :: index(value)
313
327
314
- Returns first index position of `` value ``. Raises :exc: ` ValueError ` if
315
- `` value `` is not present.
328
+ Return first index position of * value *.
329
+ Raise :exc: ` ValueError ` if * value * is not present.
316
330
317
331
.. attribute :: format
318
332
@@ -372,8 +386,8 @@ behind it:
372
386
>>> c.shm.close()
373
387
>>> c.shm.unlink()
374
388
375
- The following examples demonstrates that `` ShareableList ` `
376
- (and underlying `` SharedMemory ` `) objects
389
+ The following examples demonstrates that :class: ` ShareableList `
390
+ (and underlying :class: ` SharedMemory `) objects
377
391
can be pickled and unpickled if needed.
378
392
Note, that it will still be the same shared object.
379
393
This happens, because the deserialized object has
0 commit comments