Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
gpshead merged 28 commits intopython:mainfrompan324:shmemuntrack
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
Show all changes
28 commits
Select commitHold shift + click to select a range
3be08b4
add track parameter to shared memory
pan324Oct 12, 2023
f50812a
phrasing
pan324Oct 12, 2023
cbc8431
📜🤖 Added by blurb_it.
blurb-it[bot]Oct 12, 2023
7138447
phrasing
pan324Oct 12, 2023
6bbfca6
Merge branch 'main' of https://github.com/python/cpython into shmemun…
pan324Oct 12, 2023
db79426
Merge branch 'python:main' into shmemuntrack
pan324Oct 12, 2023
db11894
Merge branch 'shmemuntrack' of github.com:pan324/cpython into shmemun…
pan324Oct 12, 2023
d44cb82
Delete Doc/library/result.html
pan324Oct 12, 2023
c62cff0
Merge branch 'main' into shmemuntrack
ambvOct 13, 2023
dcda10f
Update Misc/NEWS.d/next/Library/2023-10-12-18-19-47.gh-issue-82300.P8…
pan324Oct 15, 2023
63d21d7
Update Doc/library/multiprocessing.shared_memory.rst
pan324Oct 15, 2023
e990e41
Update Doc/library/multiprocessing.shared_memory.rst
pan324Oct 15, 2023
9c593ba
Removed TypeError. Clarified documentation.
pan324Oct 17, 2023
9ef1ff3
untracking shmem can unlink now
pan324Oct 17, 2023
e5fe674
Update Doc/library/multiprocessing.shared_memory.rst
pan324Oct 19, 2023
66acf90
removed unneeded try-except
pan324Oct 19, 2023
3fdf625
phrasing of track parameter
pan324Oct 19, 2023
d65e3f8
Update Doc/library/multiprocessing.shared_memory.rst
pan324Oct 24, 2023
a97c6d3
untracking test
pan324Oct 24, 2023
17c07f5
untrack tests both track=True and track=False
pan324Oct 25, 2023
13f3fb6
untrack tests both track=True and track=False
pan324Oct 25, 2023
7c7f0e7
untrack tests both track=True and track=False
pan324Oct 25, 2023
765afb7
reliable test cleanup
pan324Nov 30, 2023
a5848c1
Update Doc/library/multiprocessing.shared_memory.rst
pan324Dec 1, 2023
8255e01
split tests
pan324Dec 1, 2023
5d89117
split tests
pan324Dec 1, 2023
66db5b8
style: add a missing blank line
gpsheadDec 2, 2023
52053f8
Merge branch 'main' into shmemuntrack
gpsheadDec 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
PrevPrevious commit
NextNext commit
Removed TypeError. Clarified documentation.
  • Loading branch information
@pan324
pan324 committedOct 17, 2023
commit9c593bacaac2cca7e4b02df24838b9c1a76fd094
38 changes: 21 additions & 17 deletionsDoc/library/multiprocessing.shared_memory.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -64,34 +64,38 @@ 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 withthe resource
*track*, when enabled, registers the shared memory block witha 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``.
to do so (mainly due to being killed by signals). Unless a Python process
was created using any of the :mod:`multiprocessing` facilities (such as
:class:`multiprocessing.Process`), it will receive its own resource tracker
process when accessing shared memory with *track* enabled. This will cause
the shared memory to be deleted by the resource tracker of the first
process that terminates. To avoid this issue, users of :mod:`subprocess` or
standalone Python processes should set *track* to ``False`` when there is
already another process in place that does the bookkeeping. *track* has
an effect only on POSIX. Windows has its own tracking and does not use the
resource tracker.

.. versionchanged:: 3.13 Added *track* parameter.

.. method:: close()

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.
instance. :meth:`close()` should be called once access to the shared
memory block from this 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 :meth:`unlink()` method.

.. method:: unlink()

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.
Therefore, 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.
:meth:`unlink()` and :meth:`close()` can be called in any order, but
trying to access data inside a shared memory block after :meth:`unlink()`
may result in memory access errors, depending on platform.

.. attribute:: buf

Expand Down
3 changes: 0 additions & 3 deletionsLib/multiprocessing/shared_memory.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -82,7 +82,6 @@ def __init__(self, name=None, create=False, size=0, track=True):
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 DownExpand Up@@ -240,8 +239,6 @@ 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)
Expand Down
25 changes: 25 additions & 0 deletionsLib/test/_test_multiprocessing.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -4440,6 +4440,31 @@ def test_shared_memory_cleaned_after_process_termination(self):
"resource_tracker: There appear to be 1 leaked "
"shared_memory objects to clean up at shutdown", err)

@unittest.skipIf(os.name != "posix", "resource_tracker is posix only")
def test_shared_memory_untracking(self):
# gh-82300: When a separate Python process accesses shared memory
# with track=False, it must not register with the resource tracker.
cmd = '''if 1:
import sys
from unittest.mock import Mock
from multiprocessing import resource_tracker
from multiprocessing.shared_memory import SharedMemory
resource_tracker.register = Mock(side_effect=AssertionError)
mem = SharedMemory(create=False, name=sys.argv[1], track=False)
mem.close()
'''
mem = shared_memory.SharedMemory(create=True, size=10)
try:
*_, err = test.support.script_helper.assert_python_ok("-c", cmd,
mem.name)
self.assertEqual(err, b'')
finally:
mem.close()
try:
mem.unlink()
except OSError:
pass

#
# Test to verify that `Finalize` works.
#
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp