Skip to content

Commit af85274

Browse files
Docs: Align multiprocessing.shared_memory docs with Sphinx recommendations (#114103)
- add :class: and :mod: markups where needed - fix incorrect escaping of a star in ShareableList arg spec - mark up parameters with stars: *val* - mark up list of built-in types using list markup - remove unneeded parentheses from :meth: markups
1 parent d4dfad2 commit af85274

File tree

2 files changed

+54
-47
lines changed

2 files changed

+54
-47
lines changed

Doc/library/multiprocessing.shared_memory.rst

Lines changed: 54 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ and management of shared memory to be accessed by one or more processes
2020
on a multicore or symmetric multiprocessor (SMP) machine. To assist with
2121
the life-cycle management of shared memory especially across distinct
2222
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.
2525

2626
In this module, shared memory refers to "System V style" shared memory blocks
2727
(though is not necessarily implemented explicitly as such) and does not refer
@@ -47,9 +47,9 @@ copying of data.
4747
As a resource for sharing data across processes, shared memory blocks
4848
may outlive the original process that created them. When one process
4949
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.
5151
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.
5353

5454
*name* is the unique name for the requested shared memory, specified as
5555
a string. When creating a new shared memory block, if ``None`` (the
@@ -62,7 +62,7 @@ copying of data.
6262
memory block. Because some platforms choose to allocate chunks of memory
6363
based upon that platform's memory page size, the exact size of the shared
6464
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.
6666

6767
*track*, when enabled, registers the shared memory block with a resource
6868
tracker process on platforms where the OS does not do this automatically.
@@ -86,19 +86,19 @@ copying of data.
8686
.. method:: close()
8787

8888
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
9090
memory block from this instance is no longer needed. Depending
9191
on operating system, the underlying memory may or may not be freed
9292
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.
9494

9595
.. method:: unlink()
9696

9797
Deletes the underlying shared memory block. This should be called only
9898
once per shared memory block regardless of the number of handles to it,
9999
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`
102102
may result in memory access errors, depending on platform.
103103

104104
This method has no effect on Windows, where the only way to delete a
@@ -145,7 +145,7 @@ instances::
145145

146146
The following example demonstrates a practical use of the :class:`SharedMemory`
147147
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:
149149

150150
.. doctest::
151151
:options: +SKIP
@@ -197,43 +197,43 @@ same ``numpy.ndarray`` from two distinct Python shells:
197197
.. class:: SharedMemoryManager([address[, authkey]])
198198
:module: multiprocessing.managers
199199

200-
A subclass of :class:`~multiprocessing.managers.BaseManager` which can be
200+
A subclass of :class:`multiprocessing.managers.BaseManager` which can be
201201
used for the management of shared memory blocks across processes.
202202

203203
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.
205205
This new process's sole purpose is to manage the life cycle
206206
of all shared memory blocks created through it. To trigger the release
207207
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
213213
and trigger the freeing of shared memory resources.
214214

215215
This class provides methods for creating and returning :class:`SharedMemory`
216216
instances and for creating a list-like object (:class:`ShareableList`)
217217
backed by shared memory.
218218

219-
Refer to :class:`multiprocessing.managers.BaseManager` for a description
219+
Refer to :class:`~multiprocessing.managers.BaseManager` for a description
220220
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
222222
from other processes.
223223

224224
.. method:: SharedMemory(size)
225225

226226
Create and return a new :class:`SharedMemory` object with the
227-
specified ``size`` in bytes.
227+
specified *size* in bytes.
228228

229229
.. method:: ShareableList(sequence)
230230

231231
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*.
233233

234234

235235
The following example demonstrates the basic mechanisms of a
236-
:class:`SharedMemoryManager`:
236+
:class:`~multiprocessing.managers.SharedMemoryManager`:
237237

238238
.. doctest::
239239
:options: +SKIP
@@ -251,9 +251,9 @@ The following example demonstrates the basic mechanisms of a
251251
>>> smm.shutdown() # Calls unlink() on sl, raw_shm, and another_sl
252252

253253
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:
257257

258258
.. doctest::
259259
:options: +SKIP
@@ -269,38 +269,46 @@ needed:
269269
... p2.join() # Wait for all work to complete in both processes
270270
... total_result = sum(sl) # Consolidate the partial results now in sl
271271

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.
275276

276277

277-
.. class:: ShareableList(sequence=None, \*, name=None)
278+
.. class:: ShareableList(sequence=None, *, name=None)
278279

279280
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
287295
via slicing.
288296

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.
290298
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.
292300

293301
*name* is the unique name for the requested shared memory, as described
294302
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``.
297305

298306
.. note::
299307

300308
A known issue exists for :class:`bytes` and :class:`str` values.
301309
If they end with ``\x00`` nul bytes or characters, those may be
302310
*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
304312
considered a bug and may go away in the future. See :gh:`106939`.
305313

306314
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
326334

327335
.. method:: count(value)
328336

329-
Returns the number of occurrences of ``value``.
337+
Returns the number of occurrences of *value*.
330338

331339
.. method:: index(value)
332340

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.
335343

336344
.. attribute:: format
337345

@@ -391,8 +399,8 @@ behind it:
391399
>>> c.shm.close()
392400
>>> c.shm.unlink()
393401

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
396404
can be pickled and unpickled if needed.
397405
Note, that it will still be the same shared object.
398406
This happens, because the deserialized object has

Doc/tools/.nitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ Doc/library/logging.handlers.rst
5757
Doc/library/lzma.rst
5858
Doc/library/mmap.rst
5959
Doc/library/multiprocessing.rst
60-
Doc/library/multiprocessing.shared_memory.rst
6160
Doc/library/optparse.rst
6261
Doc/library/os.rst
6362
Doc/library/pickle.rst

0 commit comments

Comments
 (0)