@@ -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
@@ -47,9 +47,9 @@ copying of data.
47
47
As a resource for sharing data across processes, shared memory blocks
48
48
may outlive the original process that created them. When one process
49
49
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.
50
+ needed by other processes, the :meth: `close ` method should be called.
51
51
When a shared memory block is no longer needed by any process, the
52
- :meth: `unlink() ` method should be called to ensure proper cleanup.
52
+ :meth: `unlink ` method should be called to ensure proper cleanup.
53
53
54
54
*name * is the unique name for the requested shared memory, specified as
55
55
a string. When creating a new shared memory block, if ``None `` (the
@@ -62,7 +62,7 @@ copying of data.
62
62
memory block. Because some platforms choose to allocate chunks of memory
63
63
based upon that platform's memory page size, the exact size of the shared
64
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.
65
+ to an existing shared memory block, the * size * parameter is ignored.
66
66
67
67
*track *, when enabled, registers the shared memory block with a resource
68
68
tracker process on platforms where the OS does not do this automatically.
@@ -86,19 +86,19 @@ copying of data.
86
86
.. method :: close()
87
87
88
88
Closes the file descriptor/handle to the shared memory from this
89
- instance. :meth: `close() ` should be called once access to the shared
89
+ instance. :meth: `close ` should be called once access to the shared
90
90
memory block from this instance is no longer needed. Depending
91
91
on operating system, the underlying memory may or may not be freed
92
92
even if all handles to it have been closed. To ensure proper cleanup,
93
- use the :meth: `unlink() ` method.
93
+ use the :meth: `unlink ` method.
94
94
95
95
.. method :: unlink()
96
96
97
97
Deletes the underlying shared memory block. This should be called only
98
98
once per shared memory block regardless of the number of handles to it,
99
99
even in other processes.
100
- :meth: `unlink() ` and :meth: `close() ` can be called in any order, but
101
- trying to access data inside a shared memory block after :meth: `unlink() `
100
+ :meth: `unlink ` and :meth: `close ` can be called in any order, but
101
+ trying to access data inside a shared memory block after :meth: `unlink `
102
102
may result in memory access errors, depending on platform.
103
103
104
104
This method has no effect on Windows, where the only way to delete a
@@ -145,7 +145,7 @@ instances::
145
145
146
146
The following example demonstrates a practical use of the :class: `SharedMemory `
147
147
class with `NumPy arrays <https://numpy.org/ >`_, accessing the
148
- same `` numpy.ndarray ` ` from two distinct Python shells:
148
+ same :class: ` ! numpy.ndarray ` from two distinct Python shells:
149
149
150
150
.. doctest ::
151
151
:options: +SKIP
@@ -197,43 +197,43 @@ same ``numpy.ndarray`` from two distinct Python shells:
197
197
.. class :: SharedMemoryManager([address[, authkey]])
198
198
:module: multiprocessing.managers
199
199
200
- A subclass of :class: `~ multiprocessing.managers.BaseManager ` which can be
200
+ A subclass of :class: `multiprocessing.managers.BaseManager ` which can be
201
201
used for the management of shared memory blocks across processes.
202
202
203
203
A call to :meth: `~multiprocessing.managers.BaseManager.start ` on a
204
- :class: `SharedMemoryManager ` instance causes a new process to be started.
204
+ :class: `! SharedMemoryManager ` instance causes a new process to be started.
205
205
This new process's sole purpose is to manage the life cycle
206
206
of all shared memory blocks created through it. To trigger the release
207
207
of all shared memory blocks managed by that process, call
208
- :meth: `~multiprocessing.managers.BaseManager.shutdown() ` on the instance.
209
- This triggers a :meth: `SharedMemory.unlink() ` call on all of the
210
- :class: `SharedMemory ` objects managed by that process and then
211
- stops the process itself. By creating `` SharedMemory ` ` instances
212
- through a `` SharedMemoryManager ` `, we avoid the need to manually track
208
+ :meth: `~multiprocessing.managers.BaseManager.shutdown ` on the instance.
209
+ This triggers a :meth: `~multiprocessing.shared_memory. SharedMemory.unlink ` call
210
+ on all of the :class: `SharedMemory ` objects managed by that process and then
211
+ stops the process itself. By creating :class: ` ! SharedMemory ` instances
212
+ through a :class: ` ! SharedMemoryManager `, we avoid the need to manually track
213
213
and trigger the freeing of shared memory resources.
214
214
215
215
This class provides methods for creating and returning :class: `SharedMemory `
216
216
instances and for creating a list-like object (:class: `ShareableList `)
217
217
backed by shared memory.
218
218
219
- Refer to :class: `multiprocessing.managers.BaseManager ` for a description
219
+ Refer to :class: `~ multiprocessing.managers.BaseManager ` for a description
220
220
of the inherited *address * and *authkey * optional input arguments and how
221
- they may be used to connect to an existing `` SharedMemoryManager ` ` service
221
+ they may be used to connect to an existing :class: ` ! SharedMemoryManager ` service
222
222
from other processes.
223
223
224
224
.. method :: SharedMemory(size)
225
225
226
226
Create and return a new :class: `SharedMemory ` object with the
227
- specified `` size `` in bytes.
227
+ specified * size * in bytes.
228
228
229
229
.. method :: ShareableList(sequence)
230
230
231
231
Create and return a new :class: `ShareableList ` object, initialized
232
- by the values from the input `` sequence `` .
232
+ by the values from the input * sequence * .
233
233
234
234
235
235
The following example demonstrates the basic mechanisms of a
236
- :class: `SharedMemoryManager `:
236
+ :class: `~multiprocessing.managers. SharedMemoryManager `:
237
237
238
238
.. doctest ::
239
239
:options: +SKIP
@@ -251,9 +251,9 @@ The following example demonstrates the basic mechanisms of a
251
251
>>> smm.shutdown() # Calls unlink() on sl, raw_shm, and another_sl
252
252
253
253
The following example depicts a potentially more convenient pattern for using
254
- :class: `SharedMemoryManager ` objects via the :keyword: ` with ` statement to
255
- ensure that all shared memory blocks are released after they are no longer
256
- needed:
254
+ :class: `~multiprocessing.managers. SharedMemoryManager ` objects via the
255
+ :keyword: ` with ` statement to ensure that all shared memory blocks are released
256
+ after they are no longer needed:
257
257
258
258
.. doctest ::
259
259
:options: +SKIP
@@ -269,38 +269,46 @@ needed:
269
269
... p2.join() # Wait for all work to complete in both processes
270
270
... total_result = sum (sl) # Consolidate the partial results now in sl
271
271
272
- When using a :class: `SharedMemoryManager ` in a :keyword: `with ` statement, the
273
- shared memory blocks created using that manager are all released when the
274
- :keyword: `with ` statement's code block finishes execution.
272
+ When using a :class: `~multiprocessing.managers.SharedMemoryManager `
273
+ in a :keyword: `with ` statement, the shared memory blocks created using that
274
+ manager are all released when the :keyword: `!with ` statement's code block
275
+ finishes execution.
275
276
276
277
277
- .. class :: ShareableList(sequence=None, \ *, name=None)
278
+ .. class :: ShareableList(sequence=None, *, name=None)
278
279
279
280
Provides a mutable list-like object where all values stored within are
280
- stored in a shared memory block. This constrains storable values to
281
- only the ``int `` (signed 64-bit), ``float ``, ``bool ``, ``str `` (less
282
- than 10M bytes each when encoded as utf-8), ``bytes `` (less than 10M
283
- bytes each), and ``None `` built-in data types. It also notably
284
- differs from the built-in ``list `` type in that these lists can not
285
- change their overall length (i.e. no append, insert, etc.) and do not
286
- support the dynamic creation of new :class: `ShareableList ` instances
281
+ stored in a shared memory block.
282
+ This constrains storable values to the following built-in data types:
283
+
284
+ * :class: `int ` (signed 64-bit)
285
+ * :class: `float `
286
+ * :class: `bool `
287
+ * :class: `str ` (less than 10M bytes each when encoded as UTF-8)
288
+ * :class: `bytes ` (less than 10M bytes each)
289
+ * ``None ``
290
+
291
+ It also notably differs from the built-in :class: `list ` type
292
+ in that these lists can not change their overall length
293
+ (i.e. no :meth: `!append `, :meth: `!insert `, etc.) and do not
294
+ support the dynamic creation of new :class: `!ShareableList ` instances
287
295
via slicing.
288
296
289
- *sequence * is used in populating a new `` ShareableList ` ` full of values.
297
+ *sequence * is used in populating a new :class: ` ! ShareableList ` full of values.
290
298
Set to ``None `` to instead attach to an already existing
291
- `` ShareableList ` ` by its unique shared memory name.
299
+ :class: ` ! ShareableList ` by its unique shared memory name.
292
300
293
301
*name * is the unique name for the requested shared memory, as described
294
302
in the definition for :class: `SharedMemory `. When attaching to an
295
- existing `` ShareableList ` `, specify its shared memory block's unique
296
- name while leaving `` sequence `` set to ``None ``.
303
+ existing :class: ` ! ShareableList `, specify its shared memory block's unique
304
+ name while leaving * sequence * set to ``None ``.
297
305
298
306
.. note ::
299
307
300
308
A known issue exists for :class: `bytes ` and :class: `str ` values.
301
309
If they end with ``\x00 `` nul bytes or characters, those may be
302
310
*silently stripped * when fetching them by index from the
303
- :class: `ShareableList `. This ``.rstrip(b'\x00') `` behavior is
311
+ :class: `! ShareableList `. This ``.rstrip(b'\x00') `` behavior is
304
312
considered a bug and may go away in the future. See :gh: `106939 `.
305
313
306
314
For applications where rstripping of trailing nulls is a problem,
@@ -326,12 +334,12 @@ shared memory blocks created using that manager are all released when the
326
334
327
335
.. method :: count(value)
328
336
329
- Returns the number of occurrences of `` value `` .
337
+ Returns the number of occurrences of * value * .
330
338
331
339
.. method :: index(value)
332
340
333
- Returns first index position of `` value `` . Raises :exc: `ValueError ` if
334
- `` value `` is not present.
341
+ Returns first index position of * value * . Raises :exc: `ValueError ` if
342
+ * value * is not present.
335
343
336
344
.. attribute :: format
337
345
@@ -391,8 +399,8 @@ behind it:
391
399
>>> c.shm.close()
392
400
>>> c.shm.unlink()
393
401
394
- The following examples demonstrates that `` ShareableList ` `
395
- (and underlying `` SharedMemory ` `) objects
402
+ The following examples demonstrates that :class: ` ShareableList `
403
+ (and underlying :class: ` SharedMemory `) objects
396
404
can be pickled and unpickled if needed.
397
405
Note, that it will still be the same shared object.
398
406
This happens, because the deserialized object has
0 commit comments