Skip to content

Commit

Permalink
pythongh-101014: support named SharedMemory and ShareableList in Shar…
Browse files Browse the repository at this point in the history
…edMemoryManager
  • Loading branch information
knwng committed Feb 23, 2023
1 parent 056dfc7 commit 98f7936
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
8 changes: 4 additions & 4 deletions Lib/multiprocessing/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1355,23 +1355,23 @@ def get_server(self):
return self._Server(self._registry, self._address,
self._authkey, self._serializer)

def SharedMemory(self, size):
def SharedMemory(self, size, name=None):
"""Returns a new SharedMemory instance with the specified size in
bytes, to be tracked by the manager."""
with self._Client(self._address, authkey=self._authkey) as conn:
sms = shared_memory.SharedMemory(None, create=True, size=size)
sms = shared_memory.SharedMemory(name, create=True, size=size)
try:
dispatch(conn, None, 'track_segment', (sms.name,))
except BaseException as e:
sms.unlink()
raise e
return sms

def ShareableList(self, sequence):
def ShareableList(self, sequence, name=None):
"""Returns a new ShareableList instance populated with the values
from the input sequence, to be tracked by the manager."""
with self._Client(self._address, authkey=self._authkey) as conn:
sl = shared_memory.ShareableList(sequence)
sl = shared_memory.ShareableList(sequence, name=name)
try:
dispatch(conn, None, 'track_segment', (sl.shm.name,))
except BaseException as e:
Expand Down
10 changes: 10 additions & 0 deletions Lib/test/_test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -4144,6 +4144,16 @@ def test_shared_memory_SharedMemoryManager_basics(self):
# No longer there to be attached to again.
absent_sl = shared_memory.ShareableList(name=held_name)

def test_shared_memory_SharedMemoryManager_named_objects(self):
shm_name = 'test_named_shared_memory'
sl_name = 'test_named_sharable_list'
with multiprocessing.managers.SharedMemoryManager() as smm:
shm = smm.SharedMemory(size=5, name=shm_name)
doppleganger_shm = shared_memory.SharedMemory(name=shm_name)
self.assertEqual(len(doppleganger_shm.buf), 5)
sl = smm.ShareableList(range(5), name=sl_name)
doppleganger_sl = shared_memory.ShareableList(name=sl_name)
self.assertEqual(len(doppleganger_sl), 5)

def test_shared_memory_ShareableList_basics(self):
sl = shared_memory.ShareableList(
Expand Down

0 comments on commit 98f7936

Please sign in to comment.