Multiprocessing.shared_memory - Shared Memory for Direct Access Throughout Processes¶ > 자유게시판

본문 바로가기

자유게시판

Multiprocessing.shared_memory - Shared Memory for Direct Access Throug…

profile_image
Antonio
2025-08-14 18:48 6 0

본문

This module provides a class, SharedMemory, for the allocation and administration of shared memory to be accessed by a number of processes on a multicore or symmetric multiprocessor (SMP) machine. To help with the life-cycle administration of shared memory particularly throughout distinct processes, a BaseManager subclass, SharedMemoryManager, can be supplied in the multiprocessing.managers module. In this module, shared memory refers to "POSIX style" shared memory blocks (although isn't necessarily applied explicitly as such) and does not check with "distributed shared memory". This fashion of shared memory permits distinct processes to doubtlessly learn and write to a standard (or shared) region of risky memory. Processes are conventionally restricted to only have entry to their very own process memory area but shared memory permits the sharing of data between processes, avoiding the necessity to as an alternative send messages between processes containing that information. Sharing knowledge immediately by way of memory can provide significant performance advantages in comparison with sharing data via disk or Memory Wave Protocol socket or different communications requiring the serialization/deserialization and copying of data.



Create an instance of the SharedMemory class for either creating a new shared memory block or attaching to an present shared memory block. Each shared memory block is assigned a unique title. In this way, one process can create a shared memory block with a selected name and a unique course of can attach to that same shared memory block using that very same name. As a resource for sharing knowledge throughout processes, shared memory blocks might outlive the unique course of that created them. When one course of now not needs access to a shared memory block which may nonetheless be wanted by other processes, the shut() methodology needs to be known as. When a shared memory block is now not needed by any course of, the unlink() technique should be known as to make sure correct cleanup. The unique name for the requested shared memory, specified as a string. When creating a brand new shared memory block, if None (the default) is equipped for the identify, a novel name might be generated.

anatomicky-tvarovany-polstar-z-pametove-peny-memory-wave-72x42-cm.jpg

Control whether or not a new shared memory block is created (True) or Memory Wave an existing shared memory block is connected (False). The requested number of bytes when creating a brand new shared memory block. Because some platforms select to allocate chunks of memory based mostly upon that platform’s memory page dimension, the precise dimension of the shared memory block may be larger or Memory Wave equal to the dimensions requested. When attaching to an present shared memory block, the dimensions parameter is ignored. When True, register the shared memory block with a resource tracker course of on platforms the place the OS doesn't do this mechanically. The resource tracker ensures correct cleanup of the shared memory even when all other processes with access to the memory exit without doing so. Python processes created from a typical ancestor utilizing multiprocessing amenities share a single resource tracker process, and the lifetime of shared memory segments is dealt with routinely amongst these processes.



Python processes created in another method will receive their own useful resource tracker when accessing shared memory with track enabled. This can trigger the shared memory to be deleted by the useful resource tracker of the first course of that terminates. To keep away from this problem, customers of subprocess or standalone Python processes ought to set monitor to False when there may be already another process in place that does the bookkeeping. Home windows, which has its personal monitoring and automatically deletes shared memory when all handles to it have been closed. Modified in version 3.13: Added the track parameter. Close the file descriptor/handle to the shared Memory Wave Protocol from this occasion. Relying on working system, the underlying memory may or will not be freed even if all handles to it have been closed. To make sure proper cleanup, use the unlink() technique. Delete the underlying shared memory block. This should be referred to as solely once per shared memory block regardless of the number of handles to it, even in other processes.



This methodology has no impact on Windows, where the only approach to delete a shared memory block is to shut all handles. A memoryview of contents of the shared memory block. Learn-only access to the unique title of the shared memory block. Read-solely entry to measurement in bytes of the shared memory block. A subclass of multiprocessing.managers.BaseManager which can be used for the management of shared memory blocks throughout processes. A call to begin() on a SharedMemoryManager instance causes a brand new course of to be began. This new process’s sole objective is to manage the life cycle of all shared memory blocks created through it. To set off the release of all shared memory blocks managed by that course of, name shutdown() on the instance. This triggers a unlink() call on all the SharedMemory objects managed by that process after which stops the process itself. By creating SharedMemory cases through a SharedMemoryManager, we avoid the necessity to manually track and set off the freeing of shared memory assets.

댓글목록0

등록된 댓글이 없습니다.

댓글쓰기

적용하기
자동등록방지 숫자를 순서대로 입력하세요.
게시판 전체검색
상담신청