Skip to content

Commit 05e26dd

Browse files
committed
Fix missing docstring on class, add test for ignoring size when attaching.
1 parent 0d3d06f commit 05e26dd

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

Lib/multiprocessing/shared_memory.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ def _make_filename():
4949

5050

5151
class SharedMemory:
52+
"""Creates a new shared memory block or attaches to an existing
53+
shared memory block.
54+
55+
Every shared memory block is assigned a unique name. This enables
56+
one process to create a shared memory block with a particular name
57+
so that a different process can attach to that same shared memory
58+
block using that same name.
59+
60+
As a resource for sharing data across processes, shared memory blocks
61+
may outlive the original process that created them. When one process
62+
no longer needs access to a shared memory block that might still be
63+
needed by other processes, the close() method should be called.
64+
When a shared memory block is no longer needed by any process, the
65+
unlink() method should be called to ensure proper cleanup."""
5266

5367
# Defaults; enables close() and unlink() to run without errors.
5468
_name = None

Lib/test/_test_multiprocessing.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3650,6 +3650,11 @@ def test_shared_memory_basics(self):
36503650
self.assertEqual(also_sms.buf[0], 42)
36513651
also_sms.close()
36523652

3653+
# Attach to existing shared memory segment but specify a new size.
3654+
same_sms = shared_memory.SharedMemory('test01_tsmb', size=20*sms.size)
3655+
self.assertEqual(same_sms.size, sms.size) # Size was ignored.
3656+
same_sms.close()
3657+
36533658
if shared_memory._USE_POSIX:
36543659
# Posix Shared Memory can only be unlinked once. Here we
36553660
# test an implementation detail that is not observed across
@@ -3675,9 +3680,6 @@ def test_shared_memory_basics(self):
36753680
finally:
36763681
sms_uno.unlink() # A second shm_unlink() call is bad.
36773682

3678-
# Enforcement of `mode` and `read_only` is OS platform dependent
3679-
# and as such will not be tested here.
3680-
36813683
with self.assertRaises(FileExistsError):
36823684
# Attempting to create a new shared memory segment with a
36833685
# name that is already in use triggers an exception.

0 commit comments

Comments
 (0)