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阅读 220,458评论 6 513
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,030评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,879评论 0 358
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,278评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,296评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,019评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,633评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,541评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,068评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,181评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,318评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,991评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,670评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,183评论 0 23
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,302评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,655评论 3 375
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,327评论 2 358