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.

這個(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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末忧勿,一起剝皮案震驚了整個(gè)濱河市杉女,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌鸳吸,老刑警劉巖熏挎,帶你破解...
    沈念sama閱讀 219,366評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異晌砾,居然都是意外死亡坎拐,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,521評論 3 395
  • 文/潘曉璐 我一進(jìn)店門养匈,熙熙樓的掌柜王于貴愁眉苦臉地迎上來哼勇,“玉大人,你說我怎么就攤上這事呕乎』#” “怎么了?”我有些...
    開封第一講書人閱讀 165,689評論 0 356
  • 文/不壞的土叔 我叫張陵猬仁,是天一觀的道長帝璧。 經(jīng)常有香客問我先誉,道長,這世上最難降的妖魔是什么的烁? 我笑而不...
    開封第一講書人閱讀 58,925評論 1 295
  • 正文 為了忘掉前任褐耳,我火速辦了婚禮,結(jié)果婚禮上渴庆,老公的妹妹穿的比我還像新娘漱病。我一直安慰自己,他們只是感情好把曼,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,942評論 6 392
  • 文/花漫 我一把揭開白布杨帽。 她就那樣靜靜地躺著,像睡著了一般嗤军。 火紅的嫁衣襯著肌膚如雪注盈。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,727評論 1 305
  • 那天叙赚,我揣著相機(jī)與錄音老客,去河邊找鬼。 笑死震叮,一個(gè)胖子當(dāng)著我的面吹牛胧砰,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播苇瓣,決...
    沈念sama閱讀 40,447評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼尉间,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了击罪?” 一聲冷哼從身側(cè)響起哲嘲,我...
    開封第一講書人閱讀 39,349評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎媳禁,沒想到半個(gè)月后眠副,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,820評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡竣稽,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,990評論 3 337
  • 正文 我和宋清朗相戀三年囱怕,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片毫别。...
    茶點(diǎn)故事閱讀 40,127評論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡娃弓,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出拧烦,到底是詐尸還是另有隱情忘闻,我是刑警寧澤,帶...
    沈念sama閱讀 35,812評論 5 346
  • 正文 年R本政府宣布恋博,位于F島的核電站齐佳,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏债沮。R本人自食惡果不足惜炼吴,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,471評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望疫衩。 院中可真熱鬧硅蹦,春花似錦、人聲如沸闷煤。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,017評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽鲤拿。三九已至假褪,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間近顷,已是汗流浹背生音。 一陣腳步聲響...
    開封第一講書人閱讀 33,142評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留窒升,地道東北人缀遍。 一個(gè)月前我還...
    沈念sama閱讀 48,388評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像饱须,于是被迫代替她去往敵國和親域醇。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,066評論 2 355