This module provides a class, SharedMemory, 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 BaseManager subclass, SharedMemoryManager, is also provided in the multiprocessing.managers module.
這個模塊提供了一個類: SharedMemory
這個類提供了用于分配和管理多核或對稱多處理器(SMP)機器上的一個或多個進程訪問的共享內存。為了協助共享內存的生命周期管理,特別是跨不同進程的管理,在 multiprocessing.manager 模塊中還提供了一個 BaseManager 子類:SharedMemoryManager。
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.
在這個模塊中,共享內存指的是 "System V風格 "的共享內存塊(盡管不一定明確用共享內存塊來實現),也不是指 "分布式共享內存"。
這種方式的共享內存允許不同的進程可以對一個公共的(或共享的)易失性內存區域進行讀寫。
傳統上進程只能訪問自己的進程內存空間,但共享內存允許進程之間共享數據,避免了在包含該數據的進程之間發送消息。與通過磁盤或套接字或其他需要數據序列化/反序列化和復制的通信方式共享數據相比,直接通過內存共享數據可以提供顯著的性能優勢。
class multiprocessing.shared_memory.SharedMemory(name=None, create=False, size=0)
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.
創建一個新的共享內存塊或連接到一個已有的共享內存塊。每個共享內存塊都被分配了一個唯一的名字,這樣一來,一個進程可以創建一個具有特定名稱的共享內存塊,而另一個進程可以使用相同的名稱連接到這個共享內存塊。通過這種方式,一個進程可以創建一個具有特定名稱的共享內存塊,而另一個進程可以使用相同的名稱連接到同一個共享內存塊。
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 close() method should be called. When a shared memory block is no longer needed by any process, the unlink() method should be called to ensure proper cleanup.
作為跨進程共享數據的資源,共享內存塊的壽命可能超過創建它們的原始進程。當一個進程不再需要訪問某個共享內存塊,而其他進程可能仍然需要該內存塊時,應該調用close()方法。當一個共享內存塊不再被任何進程需要時,應調用unlink()方法以確保適當的清理。
name is the unique name for the requested shared memory, specified as a string. When creating a new shared memory block, if None (the default) is supplied for the name, a novel name will be generated.
name是請求的共享內存的唯一名稱,格式為字符串類型。當創建一個新的共享內存塊時,如果為名稱提供了None(默認為None),將生成一個新的名稱。
create controls whether a new shared memory block is created (True) or an existing shared memory block is attached (False).
create 方法控制是否創建新的共享內存塊(True)或連接到已有的共享內存塊(False)。
size 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 size parameter is ignored.
size 指定創建新的共享內存塊時要求的字節數。由于某些平臺選擇根據該平臺的內存頁大小來分配內存塊,因此共享內存塊的確切大小可能大于或等于請求的大小。當附加到一個現有的共享內存塊時,size參數會被忽略。
close()
Closes access to the shared memory from this instance. In order to ensure proper cleanup of resources, all instances should call close() once the instance is no longer needed. Note that calling close() does not cause the shared memory block itself to be destroyed.
關閉實例對共享內存的訪問。為了保證資源的正確清理,一旦不再需要該實例,所有實例都應該調用close()。
注意,調用close()不會導致共享內存塊本身被銷毀。
unlink()
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 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 unlink() has been called may result in memory access errors. Note: the last process relinquishing its hold on a shared memory block may call unlink() and close() in either order.
請求銷毀底層共享內存塊。為了確保正確清理資源,所有需要共享內存塊的進程都應該調用unlink()一次(且僅一次)。在請求銷毀共享內存塊后,共享內存塊可能會被立即銷毀,但也可能不是立即銷毀的,這種行為在不同平臺上可能有所不同。在調用unlink()后,試圖訪問共享內存塊內的數據可能會導致內存訪問錯誤。
注意:最后一個放棄對共享內存塊的控制的進程可以依次調用unlink()和close()。
buf
A memoryview of contents of the shared memory block.
對共享內存塊內容的一覽
name
Read-only access to the unique name of the shared memory block.
訪問共享內存塊的唯一名稱(只讀)。
size
Read-only access to size in bytes of the shared memory block.
以字節為單位訪問共享內存塊的大小(只讀)。
原文鏈接:
https://docs.python.org/3/library/multiprocessing.shared_memory.html