Python Glossary Русский

all Python 3multiprocessingfunction

multiprocessing.RawArray()

shared memory

Creates a fixed-length ctypes array in shared memory with no lock. The fastest way to hand a large numeric buffer to processes.

Syntax

arr = ctx.RawArray(typecode_or_type, size_or_initializer)

Without a lock it fits a scheme where each process writes its own non-overlapping chunk, so no synchronisation is needed at all. It supports the buffer protocol, so numpy.frombuffer or memoryview work with it without copying. There is no lock argument: RawArray('i', 3, lock=True) is a TypeError. The slice arr[:] copies the data into a list.

Examples

import multiprocessing as mp
def fill_half(arr, start):
    for i in range(start, start + 2):
        arr[i] = i * 1.5
if __name__ == '__main__':
    ctx = mp.get_context('spawn')
    arr = ctx.RawArray('d', 4)
    ps = [ctx.Process(target=fill_half, args=(arr, s)) for s in (0, 2)]
    for p in ps:
        p.start()
    for p in ps:
        p.join()
    print(arr[:])  # → [0.0, 1.5, 3.0, 4.5]
    print(memoryview(arr).nbytes)  # → 32 — буфер без копирования: 4 × 8 байт

See also

Open in the full glossarydocs.python.orgReport an inaccuracy