gh-82300: Add track parameter to multiprocessing.shared_memory (#110778)

Add a track parameter to shared memory to allow resource tracking via the side-launched resource tracker process to be disabled on platforms that use it (POSIX).

This allows people who do not want automated cleanup at process exit because they are using the shared memory with processes not participating in Python's resource tracking to use the shared_memory API.

Co-authored-by: Łukasz Langa <lukasz@langa.pl>
Co-authored-by: Guido van Rossum <gvanrossum@gmail.com>
Co-authored-by: Antoine Pitrou <pitrou@free.fr>
Co-authored-by: Gregory P. Smith <greg@krypto.org>
This commit is contained in:
pan324 2023-12-05 09:11:44 +01:00 committed by GitHub
parent 9f92b31339
commit 81ee026091
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 106 additions and 23 deletions

View file

@ -71,8 +71,9 @@ class SharedMemory:
_flags = os.O_RDWR
_mode = 0o600
_prepend_leading_slash = True if _USE_POSIX else False
_track = True
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:
@ -82,6 +83,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
@ -116,8 +118,8 @@ def __init__(self, name=None, create=False, size=0):
except OSError:
self.unlink()
raise
resource_tracker.register(self._name, "shared_memory")
if self._track:
resource_tracker.register(self._name, "shared_memory")
else:
@ -236,12 +238,20 @@ def close(self):
def unlink(self):
"""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 access
to the shared memory block."""
Unlink should be called once (and only once) across all handles
which have access to the shared memory block, even if these
handles belong to different processes. Closing and unlinking may
happen in any order, but trying to access data inside a shared
memory block after unlinking may result in memory errors,
depending on platform.
This method has no effect on Windows, where the only way to
delete a shared memory block is to close all handles."""
if _USE_POSIX and self._name:
_posixshmem.shm_unlink(self._name)
resource_tracker.unregister(self._name, "shared_memory")
if self._track:
resource_tracker.unregister(self._name, "shared_memory")
_encoding = "utf8"