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.
這個(gè)模塊提供了一個(gè)類: SharedMemory
這個(gè)類提供了用于分配和管理多核或?qū)ΨQ多處理器(SMP)機(jī)器上的一個(gè)或多個(gè)進(jìn)程訪問的共享內(nèi)存乘粒。為了協(xié)助共享內(nèi)存的生命周期管理,特別是跨不同進(jìn)程的管理陆赋,在 multiprocessing.manager 模塊中還提供了一個(gè) 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.
在這個(gè)模塊中,共享內(nèi)存指的是 "System V風(fēng)格 "的共享內(nèi)存塊(盡管不一定明確用共享內(nèi)存塊來實(shí)現(xiàn))羹蚣,也不是指 "分布式共享內(nèi)存"摄职。
這種方式的共享內(nèi)存允許不同的進(jìn)程可以對一個(gè)公共的(或共享的)易失性內(nèi)存區(qū)域進(jìn)行讀寫几颜。
傳統(tǒng)上進(jìn)程只能訪問自己的進(jìn)程內(nèi)存空間脸候,但共享內(nèi)存允許進(jìn)程之間共享數(shù)據(jù)穷娱,避免了在包含該數(shù)據(jù)的進(jìn)程之間發(fā)送消息。與通過磁盤或套接字或其他需要數(shù)據(jù)序列化/反序列化和復(fù)制的通信方式共享數(shù)據(jù)相比运沦,直接通過內(nèi)存共享數(shù)據(jù)可以提供顯著的性能優(yōu)勢泵额。
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.
創(chuàng)建一個(gè)新的共享內(nèi)存塊或連接到一個(gè)已有的共享內(nèi)存塊。每個(gè)共享內(nèi)存塊都被分配了一個(gè)唯一的名字,這樣一來,一個(gè)進(jìn)程可以創(chuàng)建一個(gè)具有特定名稱的共享內(nèi)存塊知染,而另一個(gè)進(jìn)程可以使用相同的名稱連接到這個(gè)共享內(nèi)存塊。通過這種方式羞秤,一個(gè)進(jìn)程可以創(chuàng)建一個(gè)具有特定名稱的共享內(nèi)存塊,而另一個(gè)進(jìn)程可以使用相同的名稱連接到同一個(gè)共享內(nèi)存塊左敌。
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.
作為跨進(jìn)程共享數(shù)據(jù)的資源瘾蛋,共享內(nèi)存塊的壽命可能超過創(chuàng)建它們的原始進(jìn)程。當(dāng)一個(gè)進(jìn)程不再需要訪問某個(gè)共享內(nèi)存塊母谎,而其他進(jìn)程可能仍然需要該內(nèi)存塊時(shí)瘦黑,應(yīng)該調(diào)用close()方法京革。當(dāng)一個(gè)共享內(nèi)存塊不再被任何進(jìn)程需要時(shí)奇唤,應(yīng)調(diào)用unlink()方法以確保適當(dāng)?shù)那謇怼?/p>
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是請求的共享內(nèi)存的唯一名稱,格式為字符串類型匹摇。當(dāng)創(chuàng)建一個(gè)新的共享內(nèi)存塊時(shí)咬扇,如果為名稱提供了None(默認(rèn)為None),將生成一個(gè)新的名稱廊勃。
create controls whether a new shared memory block is created (True) or an existing shared memory block is attached (False).
create 方法控制是否創(chuàng)建新的共享內(nèi)存塊(True)或連接到已有的共享內(nèi)存塊(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 指定創(chuàng)建新的共享內(nèi)存塊時(shí)要求的字節(jié)數(shù)经窖。由于某些平臺選擇根據(jù)該平臺的內(nèi)存頁大小來分配內(nèi)存塊,因此共享內(nèi)存塊的確切大小可能大于或等于請求的大小梭灿。當(dāng)附加到一個(gè)現(xiàn)有的共享內(nèi)存塊時(shí)画侣,size參數(shù)會(huì)被忽略。
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.
關(guān)閉實(shí)例對共享內(nèi)存的訪問堡妒。為了保證資源的正確清理配乱,一旦不再需要該實(shí)例,所有實(shí)例都應(yīng)該調(diào)用close()皮迟。
注意搬泥,調(diào)用close()不會(huì)導(dǎo)致共享內(nèi)存塊本身被銷毀。
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.
請求銷毀底層共享內(nèi)存塊伏尼。為了確保正確清理資源忿檩,所有需要共享內(nèi)存塊的進(jìn)程都應(yīng)該調(diào)用unlink()一次(且僅一次)。在請求銷毀共享內(nèi)存塊后爆阶,共享內(nèi)存塊可能會(huì)被立即銷毀燥透,但也可能不是立即銷毀的,這種行為在不同平臺上可能有所不同辨图。在調(diào)用unlink()后兽掰,試圖訪問共享內(nèi)存塊內(nèi)的數(shù)據(jù)可能會(huì)導(dǎo)致內(nèi)存訪問錯(cuò)誤。
注意:最后一個(gè)放棄對共享內(nèi)存塊的控制的進(jìn)程可以依次調(diào)用unlink()和close()徒役。
buf
A memoryview of contents of the shared memory block.
對共享內(nèi)存塊內(nèi)容的一覽
name
Read-only access to the unique name of the shared memory block.
訪問共享內(nèi)存塊的唯一名稱(只讀)孽尽。
size
Read-only access to size in bytes of the shared memory block.
以字節(jié)為單位訪問共享內(nèi)存塊的大小(只讀)。
原文鏈接:
https://docs.python.org/3/library/multiprocessing.shared_memory.html