diff options
| author | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
|---|---|---|
| committer | Craig Jennings <c@cjennings.net> | 2024-04-07 13:41:34 -0500 |
| commit | 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch) | |
| tree | f1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/python~3.12/library%2Fmultiprocessing.shared_memory.html | |
new repository
Diffstat (limited to 'devdocs/python~3.12/library%2Fmultiprocessing.shared_memory.html')
| -rw-r--r-- | devdocs/python~3.12/library%2Fmultiprocessing.shared_memory.html | 199 |
1 files changed, 199 insertions, 0 deletions
diff --git a/devdocs/python~3.12/library%2Fmultiprocessing.shared_memory.html b/devdocs/python~3.12/library%2Fmultiprocessing.shared_memory.html new file mode 100644 index 00000000..b2b87845 --- /dev/null +++ b/devdocs/python~3.12/library%2Fmultiprocessing.shared_memory.html @@ -0,0 +1,199 @@ + <span id="multiprocessing-shared-memory-shared-memory-for-direct-access-across-processes"></span><h1>multiprocessing.shared_memory — Shared memory for direct access across processes</h1> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/multiprocessing/shared_memory.py">Lib/multiprocessing/shared_memory.py</a></p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.8.</span></p> </div> <p>This module provides a class, <a class="reference internal" href="#multiprocessing.shared_memory.SharedMemory" title="multiprocessing.shared_memory.SharedMemory"><code>SharedMemory</code></a>, for the allocation and management of shared memory to be accessed by one or more processes on a multicore or symmetric multiprocessor (SMP) machine. To assist with the life-cycle management of shared memory especially across distinct processes, a <a class="reference internal" href="multiprocessing#multiprocessing.managers.BaseManager" title="multiprocessing.managers.BaseManager"><code>BaseManager</code></a> subclass, <code>SharedMemoryManager</code>, is also provided in the <code>multiprocessing.managers</code> module.</p> <p>In this module, shared memory refers to “System V style” shared memory blocks (though is not necessarily implemented explicitly as such) and does not refer to “distributed shared memory”. This style of shared memory permits distinct processes to potentially read and write to a common (or shared) region of volatile memory. Processes are conventionally limited to only have access to their own process memory space but shared memory permits the sharing of data between processes, avoiding the need to instead send messages between processes containing that data. Sharing data directly via memory can provide significant performance benefits compared to sharing data via disk or socket or other communications requiring the serialization/deserialization and copying of data.</p> <dl class="py class"> <dt class="sig sig-object py" id="multiprocessing.shared_memory.SharedMemory"> +<code>class multiprocessing.shared_memory.SharedMemory(name=None, create=False, size=0)</code> </dt> <dd> +<p>Creates a new shared memory block or attaches to an existing shared memory block. Each shared memory block is assigned a unique name. In this way, one process can create a shared memory block with a particular name and a different process can attach to that same shared memory block using that same name.</p> <p>As a resource for sharing data across processes, shared memory blocks may outlive the original process that created them. When one process no longer needs access to a shared memory block that might still be needed by other processes, the <a class="reference internal" href="#multiprocessing.shared_memory.SharedMemory.close" title="multiprocessing.shared_memory.SharedMemory.close"><code>close()</code></a> method should be called. When a shared memory block is no longer needed by any process, the <a class="reference internal" href="#multiprocessing.shared_memory.SharedMemory.unlink" title="multiprocessing.shared_memory.SharedMemory.unlink"><code>unlink()</code></a> method should be called to ensure proper cleanup.</p> <p><em>name</em> is the unique name for the requested shared memory, specified as a string. When creating a new shared memory block, if <code>None</code> (the default) is supplied for the name, a novel name will be generated.</p> <p><em>create</em> controls whether a new shared memory block is created (<code>True</code>) or an existing shared memory block is attached (<code>False</code>).</p> <p><em>size</em> specifies the requested number of bytes when creating a new shared memory block. Because some platforms choose to allocate chunks of memory based upon that platform’s memory page size, the exact size of the shared memory block may be larger or equal to the size requested. When attaching to an existing shared memory block, the <code>size</code> parameter is ignored.</p> <dl class="py method"> <dt class="sig sig-object py" id="multiprocessing.shared_memory.SharedMemory.close"> +<code>close()</code> </dt> <dd> +<p>Closes access to the shared memory from this instance. In order to ensure proper cleanup of resources, all instances should call <code>close()</code> once the instance is no longer needed. Note that calling <code>close()</code> does not cause the shared memory block itself to be destroyed.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="multiprocessing.shared_memory.SharedMemory.unlink"> +<code>unlink()</code> </dt> <dd> +<p>Requests that the underlying shared memory block be destroyed. In order to ensure proper cleanup of resources, <code>unlink()</code> 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 <code>unlink()</code> has been called may result in memory access errors. Note: the last process relinquishing its hold on a shared memory block may call <code>unlink()</code> and <a class="reference internal" href="#multiprocessing.shared_memory.SharedMemory.close" title="multiprocessing.shared_memory.SharedMemory.close"><code>close()</code></a> in either order.</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="multiprocessing.shared_memory.SharedMemory.buf"> +<code>buf</code> </dt> <dd> +<p>A memoryview of contents of the shared memory block.</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="multiprocessing.shared_memory.SharedMemory.name"> +<code>name</code> </dt> <dd> +<p>Read-only access to the unique name of the shared memory block.</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="multiprocessing.shared_memory.SharedMemory.size"> +<code>size</code> </dt> <dd> +<p>Read-only access to size in bytes of the shared memory block.</p> </dd> +</dl> </dd> +</dl> <p>The following example demonstrates low-level use of <a class="reference internal" href="#multiprocessing.shared_memory.SharedMemory" title="multiprocessing.shared_memory.SharedMemory"><code>SharedMemory</code></a> instances:</p> <pre data-language="python">>>> from multiprocessing import shared_memory +>>> shm_a = shared_memory.SharedMemory(create=True, size=10) +>>> type(shm_a.buf) +<class 'memoryview'> +>>> buffer = shm_a.buf +>>> len(buffer) +10 +>>> buffer[:4] = bytearray([22, 33, 44, 55]) # Modify multiple at once +>>> buffer[4] = 100 # Modify single byte at a time +>>> # Attach to an existing shared memory block +>>> shm_b = shared_memory.SharedMemory(shm_a.name) +>>> import array +>>> array.array('b', shm_b.buf[:5]) # Copy the data into a new array.array +array('b', [22, 33, 44, 55, 100]) +>>> shm_b.buf[:5] = b'howdy' # Modify via shm_b using bytes +>>> bytes(shm_a.buf[:5]) # Access via shm_a +b'howdy' +>>> shm_b.close() # Close each SharedMemory instance +>>> shm_a.close() +>>> shm_a.unlink() # Call unlink only once to release the shared memory +</pre> <p>The following example demonstrates a practical use of the <a class="reference internal" href="#multiprocessing.shared_memory.SharedMemory" title="multiprocessing.shared_memory.SharedMemory"><code>SharedMemory</code></a> class with <a class="reference external" href="https://numpy.org/">NumPy arrays</a>, accessing the same <code>numpy.ndarray</code> from two distinct Python shells:</p> <pre data-language="pycon3">>>> # In the first Python interactive shell +>>> import numpy as np +>>> a = np.array([1, 1, 2, 3, 5, 8]) # Start with an existing NumPy array +>>> from multiprocessing import shared_memory +>>> shm = shared_memory.SharedMemory(create=True, size=a.nbytes) +>>> # Now create a NumPy array backed by shared memory +>>> b = np.ndarray(a.shape, dtype=a.dtype, buffer=shm.buf) +>>> b[:] = a[:] # Copy the original data into shared memory +>>> b +array([1, 1, 2, 3, 5, 8]) +>>> type(b) +<class 'numpy.ndarray'> +>>> type(a) +<class 'numpy.ndarray'> +>>> shm.name # We did not specify a name so one was chosen for us +'psm_21467_46075' + +>>> # In either the same shell or a new Python shell on the same machine +>>> import numpy as np +>>> from multiprocessing import shared_memory +>>> # Attach to the existing shared memory block +>>> existing_shm = shared_memory.SharedMemory(name='psm_21467_46075') +>>> # Note that a.shape is (6,) and a.dtype is np.int64 in this example +>>> c = np.ndarray((6,), dtype=np.int64, buffer=existing_shm.buf) +>>> c +array([1, 1, 2, 3, 5, 8]) +>>> c[-1] = 888 +>>> c +array([ 1, 1, 2, 3, 5, 888]) + +>>> # Back in the first Python interactive shell, b reflects this change +>>> b +array([ 1, 1, 2, 3, 5, 888]) + +>>> # Clean up from within the second Python shell +>>> del c # Unnecessary; merely emphasizing the array is no longer used +>>> existing_shm.close() + +>>> # Clean up from within the first Python shell +>>> del b # Unnecessary; merely emphasizing the array is no longer used +>>> shm.close() +>>> shm.unlink() # Free and release the shared memory block at the very end +</pre> <dl class="py class"> <dt class="sig sig-object py" id="multiprocessing.managers.SharedMemoryManager"> +<code>class multiprocessing.managers.SharedMemoryManager([address[, authkey]])</code> </dt> <dd> +<p>A subclass of <a class="reference internal" href="multiprocessing#multiprocessing.managers.BaseManager" title="multiprocessing.managers.BaseManager"><code>BaseManager</code></a> which can be used for the management of shared memory blocks across processes.</p> <p>A call to <a class="reference internal" href="multiprocessing#multiprocessing.managers.BaseManager.start" title="multiprocessing.managers.BaseManager.start"><code>start()</code></a> on a <a class="reference internal" href="#multiprocessing.managers.SharedMemoryManager" title="multiprocessing.managers.SharedMemoryManager"><code>SharedMemoryManager</code></a> instance causes a new process to be started. This new process’s sole purpose is to manage the life cycle of all shared memory blocks created through it. To trigger the release of all shared memory blocks managed by that process, call <a class="reference internal" href="multiprocessing#multiprocessing.managers.BaseManager.shutdown" title="multiprocessing.managers.BaseManager.shutdown"><code>shutdown()</code></a> on the instance. This triggers a <code>SharedMemory.unlink()</code> call on all of the <a class="reference internal" href="#multiprocessing.managers.SharedMemoryManager.SharedMemory" title="multiprocessing.managers.SharedMemoryManager.SharedMemory"><code>SharedMemory</code></a> objects managed by that process and then stops the process itself. By creating <code>SharedMemory</code> instances through a <code>SharedMemoryManager</code>, we avoid the need to manually track and trigger the freeing of shared memory resources.</p> <p>This class provides methods for creating and returning <a class="reference internal" href="#multiprocessing.managers.SharedMemoryManager.SharedMemory" title="multiprocessing.managers.SharedMemoryManager.SharedMemory"><code>SharedMemory</code></a> instances and for creating a list-like object (<a class="reference internal" href="#multiprocessing.managers.SharedMemoryManager.ShareableList" title="multiprocessing.managers.SharedMemoryManager.ShareableList"><code>ShareableList</code></a>) backed by shared memory.</p> <p>Refer to <a class="reference internal" href="multiprocessing#multiprocessing.managers.BaseManager" title="multiprocessing.managers.BaseManager"><code>multiprocessing.managers.BaseManager</code></a> for a description of the inherited <em>address</em> and <em>authkey</em> optional input arguments and how they may be used to connect to an existing <code>SharedMemoryManager</code> service from other processes.</p> <dl class="py method"> <dt class="sig sig-object py" id="multiprocessing.managers.SharedMemoryManager.SharedMemory"> +<code>SharedMemory(size)</code> </dt> <dd> +<p>Create and return a new <a class="reference internal" href="#multiprocessing.managers.SharedMemoryManager.SharedMemory" title="multiprocessing.managers.SharedMemoryManager.SharedMemory"><code>SharedMemory</code></a> object with the specified <code>size</code> in bytes.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="multiprocessing.managers.SharedMemoryManager.ShareableList"> +<code>ShareableList(sequence)</code> </dt> <dd> +<p>Create and return a new <a class="reference internal" href="#multiprocessing.managers.SharedMemoryManager.ShareableList" title="multiprocessing.managers.SharedMemoryManager.ShareableList"><code>ShareableList</code></a> object, initialized by the values from the input <code>sequence</code>.</p> </dd> +</dl> </dd> +</dl> <p>The following example demonstrates the basic mechanisms of a <code>SharedMemoryManager</code>:</p> <pre data-language="pycon3">>>> from multiprocessing.managers import SharedMemoryManager +>>> smm = SharedMemoryManager() +>>> smm.start() # Start the process that manages the shared memory blocks +>>> sl = smm.ShareableList(range(4)) +>>> sl +ShareableList([0, 1, 2, 3], name='psm_6572_7512') +>>> raw_shm = smm.SharedMemory(size=128) +>>> another_sl = smm.ShareableList('alpha') +>>> another_sl +ShareableList(['a', 'l', 'p', 'h', 'a'], name='psm_6572_12221') +>>> smm.shutdown() # Calls unlink() on sl, raw_shm, and another_sl +</pre> <p>The following example depicts a potentially more convenient pattern for using <code>SharedMemoryManager</code> objects via the <a class="reference internal" href="../reference/compound_stmts#with"><code>with</code></a> statement to ensure that all shared memory blocks are released after they are no longer needed:</p> <pre data-language="pycon3">>>> with SharedMemoryManager() as smm: +... sl = smm.ShareableList(range(2000)) +... # Divide the work among two processes, storing partial results in sl +... p1 = Process(target=do_work, args=(sl, 0, 1000)) +... p2 = Process(target=do_work, args=(sl, 1000, 2000)) +... p1.start() +... p2.start() # A multiprocessing.Pool might be more efficient +... p1.join() +... p2.join() # Wait for all work to complete in both processes +... total_result = sum(sl) # Consolidate the partial results now in sl +</pre> <p>When using a <code>SharedMemoryManager</code> in a <a class="reference internal" href="../reference/compound_stmts#with"><code>with</code></a> statement, the shared memory blocks created using that manager are all released when the <a class="reference internal" href="../reference/compound_stmts#with"><code>with</code></a> statement’s code block finishes execution.</p> <dl class="py class"> <dt class="sig sig-object py" id="multiprocessing.shared_memory.ShareableList"> +<code>class multiprocessing.shared_memory.ShareableList(sequence=None, \*, name=None)</code> </dt> <dd> +<p>Provides a mutable list-like object where all values stored within are stored in a shared memory block. This constrains storable values to only the <code>int</code> (signed 64-bit), <code>float</code>, <code>bool</code>, <code>str</code> (less than 10M bytes each when encoded as utf-8), <code>bytes</code> (less than 10M bytes each), and <code>None</code> built-in data types. It also notably differs from the built-in <code>list</code> type in that these lists can not change their overall length (i.e. no append, insert, etc.) and do not support the dynamic creation of new <a class="reference internal" href="#multiprocessing.shared_memory.ShareableList" title="multiprocessing.shared_memory.ShareableList"><code>ShareableList</code></a> instances via slicing.</p> <p><em>sequence</em> is used in populating a new <code>ShareableList</code> full of values. Set to <code>None</code> to instead attach to an already existing <code>ShareableList</code> by its unique shared memory name.</p> <p><em>name</em> is the unique name for the requested shared memory, as described in the definition for <a class="reference internal" href="#multiprocessing.shared_memory.SharedMemory" title="multiprocessing.shared_memory.SharedMemory"><code>SharedMemory</code></a>. When attaching to an existing <code>ShareableList</code>, specify its shared memory block’s unique name while leaving <code>sequence</code> set to <code>None</code>.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>A known issue exists for <a class="reference internal" href="stdtypes#bytes" title="bytes"><code>bytes</code></a> and <a class="reference internal" href="stdtypes#str" title="str"><code>str</code></a> values. If they end with <code>\x00</code> nul bytes or characters, those may be <em>silently stripped</em> when fetching them by index from the <a class="reference internal" href="#multiprocessing.shared_memory.ShareableList" title="multiprocessing.shared_memory.ShareableList"><code>ShareableList</code></a>. This <code>.rstrip(b'\x00')</code> behavior is considered a bug and may go away in the future. See <a class="reference external" href="https://github.com/python/cpython/issues/106939">gh-106939</a>.</p> </div> <p>For applications where rstripping of trailing nulls is a problem, work around it by always unconditionally appending an extra non-0 byte to the end of such values when storing and unconditionally removing it when fetching:</p> <pre data-language="pycon3">>>> from multiprocessing import shared_memory +>>> nul_bug_demo = shared_memory.ShareableList(['?\x00', b'\x03\x02\x01\x00\x00\x00']) +>>> nul_bug_demo[0] +'?' +>>> nul_bug_demo[1] +b'\x03\x02\x01' +>>> nul_bug_demo.shm.unlink() +>>> padded = shared_memory.ShareableList(['?\x00\x07', b'\x03\x02\x01\x00\x00\x00\x07']) +>>> padded[0][:-1] +'?\x00' +>>> padded[1][:-1] +b'\x03\x02\x01\x00\x00\x00' +>>> padded.shm.unlink() +</pre> <dl class="py method"> <dt class="sig sig-object py" id="multiprocessing.shared_memory.ShareableList.count"> +<code>count(value)</code> </dt> <dd> +<p>Returns the number of occurrences of <code>value</code>.</p> </dd> +</dl> <dl class="py method"> <dt class="sig sig-object py" id="multiprocessing.shared_memory.ShareableList.index"> +<code>index(value)</code> </dt> <dd> +<p>Returns first index position of <code>value</code>. Raises <a class="reference internal" href="exceptions#ValueError" title="ValueError"><code>ValueError</code></a> if <code>value</code> is not present.</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="multiprocessing.shared_memory.ShareableList.format"> +<code>format</code> </dt> <dd> +<p>Read-only attribute containing the <a class="reference internal" href="struct#module-struct" title="struct: Interpret bytes as packed binary data."><code>struct</code></a> packing format used by all currently stored values.</p> </dd> +</dl> <dl class="py attribute"> <dt class="sig sig-object py" id="multiprocessing.shared_memory.ShareableList.shm"> +<code>shm</code> </dt> <dd> +<p>The <a class="reference internal" href="#multiprocessing.shared_memory.SharedMemory" title="multiprocessing.shared_memory.SharedMemory"><code>SharedMemory</code></a> instance where the values are stored.</p> </dd> +</dl> </dd> +</dl> <p>The following example demonstrates basic use of a <a class="reference internal" href="#multiprocessing.shared_memory.ShareableList" title="multiprocessing.shared_memory.ShareableList"><code>ShareableList</code></a> instance:</p> <pre data-language="python">>>> from multiprocessing import shared_memory +>>> a = shared_memory.ShareableList(['howdy', b'HoWdY', -273.154, 100, None, True, 42]) +>>> [ type(entry) for entry in a ] +[<class 'str'>, <class 'bytes'>, <class 'float'>, <class 'int'>, <class 'NoneType'>, <class 'bool'>, <class 'int'>] +>>> a[2] +-273.154 +>>> a[2] = -78.5 +>>> a[2] +-78.5 +>>> a[2] = 'dry ice' # Changing data types is supported as well +>>> a[2] +'dry ice' +>>> a[2] = 'larger than previously allocated storage space' +Traceback (most recent call last): + ... +ValueError: exceeds available storage for existing str +>>> a[2] +'dry ice' +>>> len(a) +7 +>>> a.index(42) +6 +>>> a.count(b'howdy') +0 +>>> a.count(b'HoWdY') +1 +>>> a.shm.close() +>>> a.shm.unlink() +>>> del a # Use of a ShareableList after call to unlink() is unsupported +</pre> <p>The following example depicts how one, two, or many processes may access the same <a class="reference internal" href="#multiprocessing.shared_memory.ShareableList" title="multiprocessing.shared_memory.ShareableList"><code>ShareableList</code></a> by supplying the name of the shared memory block behind it:</p> <pre data-language="python">>>> b = shared_memory.ShareableList(range(5)) # In a first process +>>> c = shared_memory.ShareableList(name=b.shm.name) # In a second process +>>> c +ShareableList([0, 1, 2, 3, 4], name='...') +>>> c[-1] = -999 +>>> b[-1] +-999 +>>> b.shm.close() +>>> c.shm.close() +>>> c.shm.unlink() +</pre> <p>The following examples demonstrates that <code>ShareableList</code> (and underlying <code>SharedMemory</code>) objects can be pickled and unpickled if needed. Note, that it will still be the same shared object. This happens, because the deserialized object has the same unique name and is just attached to an existing object with the same name (if the object is still alive):</p> <pre data-language="python">>>> import pickle +>>> from multiprocessing import shared_memory +>>> sl = shared_memory.ShareableList(range(10)) +>>> list(sl) +[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] +</pre> <pre data-language="python">>>> deserialized_sl = pickle.loads(pickle.dumps(sl)) +>>> list(deserialized_sl) +[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] +</pre> <pre data-language="python">>>> sl[0] = -1 +>>> deserialized_sl[1] = -2 +>>> list(sl) +[-1, -2, 2, 3, 4, 5, 6, 7, 8, 9] +>>> list(deserialized_sl) +[-1, -2, 2, 3, 4, 5, 6, 7, 8, 9] +</pre> <pre data-language="python">>>> sl.shm.close() +>>> sl.shm.unlink() +</pre> <div class="_attribution"> + <p class="_attribution-p"> + © 2001–2023 Python Software Foundation<br>Licensed under the PSF License.<br> + <a href="https://docs.python.org/3.12/library/multiprocessing.shared_memory.html" class="_attribution-link">https://docs.python.org/3.12/library/multiprocessing.shared_memory.html</a> + </p> +</div> |
