8000 gh-82300: Add track parameter to shared memory by pan324 · Pull Request #110778 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-82300: Add track parameter to shared memory #110778

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 28 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
3be08b4
add track parameter to shared memory
pan324 Oct 12, 2023
f50812a
phrasing
pan324 Oct 12, 2023
cbc8431
📜🤖 Added by blurb_it.
blurb-it[bot] Oct 12, 2023
7138447
phrasing
pan324 Oct 12, 2023
6bbfca6
Merge branch 'main' of https://github.com/python/cpython into shmemun…
pan324 Oct 12, 2023
db79426
Merge branch 'python:main' into shmemuntrack
pan324 Oct 12, 2023
db11894
Merge branch 'shmemuntrack' of github.com:pan324/cpython into shmemun…
pan324 Oct 12, 2023
d44cb82
Delete Doc/library/result.html
pan324 Oct 12, 2023
c62cff0
Merge branch 'main' into shmemuntrack
ambv Oct 13, 2023
dcda10f
Update Misc/NEWS.d/next/Library/2023-10-12-18-19-47.gh-issue-82300.P8…
pan324 Oct 15, 2023
63d21d7
Update Doc/library/multiprocessing.shared_memory.rst
pan324 Oct 15, 2023
e990e41
Update Doc/library/multiprocessing.shared_memory.rst
pan324 Oct 15, 2023
9c593ba
Removed TypeError. Clarified documentation.
pan324 Oct 17, 2023
9ef1ff3
untracking shmem can unlink now
pan324 Oct 17, 2023
e5fe674
Update Doc/library/multiprocessing.shared_memory.rst
pan324 < 8000 relative-time datetime="2023-10-19T11:11:35Z" class="no-wrap">Oct 19, 2023
66acf90
removed unneeded try-except
pan324 Oct 19, 2023
3fdf625
phrasing of track parameter
pan324 Oct 19, 2023
d65e3f8
Update Doc/library/multiprocessing.shared_memory.rst
pan324 Oct 24, 2023
a97c6d3
untracking test
pan324 Oct 24, 2023
17c07f5
untrack tests both track=True and track=False
pan324 Oct 25, 2023
13f3fb6
untrack tests both track=True and track=False
pan324 Oct 25, 2023
7c7f0e7
untrack tests both track=True and track=False
pan324 Oct 25, 2023
765afb7
reliable test cleanup
pan324 Nov 30, 2023
a5848c1
Update Doc/library/multiprocessing.shared_memory.rst
pan324 Dec 1, 2023
8255e01
split tests
pan324 Dec 1, 2023
5d89117
split tests
pan324 Dec 1, 2023
66db5b8
style: add a missing blank line
gpshead Dec 2, 2023
52053f8
Merge branch 'main' into shmemuntrack
gpshead Dec 2, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 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,26 +64,34 @@ 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 might 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.
Closes the file descriptor/handle to the shared memory from this
instance. All instances should call ``close()`` once the instance
is no longer needed. Depending on operating system, the underlying
memory may or may not be freed even if all handles to it have been
closed. To ensure proper cleanup, use the ``unlink()`` method.

.. method:: unlink()

Requests that the underlying shared memory block be destroyed. In
order to ensure proper cleanup of resources, ``unlink()`` should be
called once (and only once) across all processes which have need
for the shared memory block. After requesting its destruction, a
shared memory block may or may not be immediately destroyed and
this behavior may differ across platforms. Attempts to access data
inside the shared memory block after ``unlink()`` has been called may
result in memory access errors. Note: the last process relinquishing
its hold on a shared memory block may call ``unlink()`` and
:meth:`close()` in either order.
Deletes the underlying shared memory block. This should be called only
once per shared memory block regardless of the number of handles to it.
After requesting its deletion, a shared memory block may or may not be
immediately destroyed and this behavior may differ across platforms.
Attempts to access data inside the shared memory block after
``unlink()`` has been called may result in memory access errors.
To ensure proper bookkeeping, ``unlink()`` may only be called by
an instance with *track* enabled.

.. attribute:: buf

Expand Down
10 changes: 7 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 All @@ -82,6 +82,7 @@ def __init__(self, name=None, create=False, size=0):
if name is None and not self._flags & os.O_EXCL:
raise ValueError("'name' can only be None if create=True")

self._track = track
if _USE_POSIX:

# POSIX Shared Memory
Expand Down Expand Up @@ -116,8 +117,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 Expand Up @@ -239,6 +240,9 @@ def unlink(self):
In order to ensure proper cleanup of resources, unlink should be
called once (and only once) across all processes which have access
to the shared memory block."""
if not self._track:
raise TypeError("unlink() must be called by a tracking instance")

if _USE_POSIX and self._name:
_posixshmem.shm_unlink(self._name)
resource_tracker.unregister(self._name, "shared_memory")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add ``track`` parameter that allows using shared memory blocks without having to register with the resource tracker.
0