Python Glossary Русский

all Python 3multiprocessingfunction

multiprocessing.RawValue()

shared memory

Creates a ctypes value in shared memory with no lock: a bare object such as c_int. Faster than Value, but access safety is up to you.

Syntax

v = ctx.RawValue(typecode_or_type, *args)

It suits one writer with read-only readers, or cases where synchronisation already exists outside (your own Lock). Parallel v.value += 1 without a lock silently loses increments. Unlike Value, the result has no get_lock(): it is the same kind of object as Value(..., lock=False). It is documented in multiprocessing.sharedctypes and exposed at the module root as a context function.

Examples

import multiprocessing as mp
def set_answer(v):
    v.value = 42
if __name__ == '__main__':
    ctx = mp.get_context('spawn')
    v = ctx.RawValue('i', 0)
    print(type(v).__name__)  # → c_int
    p = ctx.Process(target=set_answer, args=(v,))
    p.start()
    p.join()
    print(v.value)  # → 42

See also

Open in the full glossarydocs.python.orgReport an inaccuracy