multiprocessing.shared_memory 模塊

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

?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,663評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,125評論 3 414
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,506評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,614評論 1 307
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,402評論 6 404
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 54,934評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,021評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,168評論 0 287
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,690評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,596評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,784評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,288評論 5 357
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,027評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,404評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,662評論 1 280
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,398評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,743評論 2 370