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

你可能感兴趣的:(multiprocessing.shared_memory 模块)