Skip to content

Commit

Permalink
add track parameter to shared memory
Browse files Browse the repository at this point in the history
  • Loading branch information
pan324 committed Oct 12, 2023
1 parent e733136 commit 3be08b4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
20 changes: 15 additions & 5 deletions Doc/library/multiprocessing.shared_memory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ or other communications requiring the serialization/deserialization and
copying of data.


.. class:: SharedMemory(name=None, create=False, size=0)
.. class:: SharedMemory(name=None, create=False, size=0, track=True)

Creates a new shared memory block or attaches to an existing shared
memory block. Each shared memory block is assigned a unique name.
Expand Down Expand Up @@ -64,13 +64,23 @@ copying of data.
memory block may be larger or equal to the size requested. When attaching
to an existing shared memory block, the ``size`` parameter is ignored.

*track*, when enabled, registers the shared memory block with the resource
tracker process. This process ensures proper cleanup of shared memory
blocks even when all other processes with access to the memory have failed
to do so (mainly due to being killed by signals). The resource tracker is
overzealous in certain situations and will delete a shared memory block
when any process with access to the shared memory has terminated. *track*
should be set to ``False`` if there is already another process in place
that does the bookkeeping. In most situations, this means that *track*
should be set to ``False`` when *create* is set to ``False``.

.. method:: close()

Closes access to the shared memory from this instance. In order to
ensure proper cleanup of resources, all instances should call
``close()`` once the instance is no longer needed. Note that calling
``close()`` does not cause the shared memory block itself to be
destroyed.
ensure proper cleanup of resources, all instances with *track* set to
``True`` should call ``close()`` once the instance is no longer needed.
Note that calling ``close()`` does not cause the shared memory block
itself to be destroyed.

.. method:: unlink()

Expand Down
6 changes: 3 additions & 3 deletions Lib/multiprocessing/shared_memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class SharedMemory:
_mode = 0o600
_prepend_leading_slash = True if _USE_POSIX else False

def __init__(self, name=None, create=False, size=0):
def __init__(self, name=None, create=False, size=0, track=True):
if not size >= 0:
raise ValueError("'size' must be a positive integer")
if create:
Expand Down Expand Up @@ -116,8 +116,8 @@ def __init__(self, name=None, create=False, size=0):
except OSError:
self.unlink()
raise

resource_tracker.register(self._name, "shared_memory")
if track:
resource_tracker.register(self._name, "shared_memory")

else:

Expand Down

0 comments on commit 3be08b4

Please sign in to comment.