Python Glossary Русский

all Python 3multiprocessingterm

multiprocessing.RLock

synchronization

A recursive lock shared between processes: the same owner may acquire it again and must release it the same number of times.

Syntax

rlock = ctx.RLock(); with rlock: ...

The owner is a process-plus-thread pair, and only it may release the RLock: anyone else's release() raises AssertionError, not RuntimeError as with threading.RLock. An RLock is what Value, Array and Condition use by default, so with v.get_lock() nested inside another with v.get_lock() does not hang. Pass the primitive to the child as a Process argument at creation; it cannot go through a Queue or Pool arguments — RuntimeError "should only be shared between processes through inheritance".

Examples

import multiprocessing as mp
if __name__ == '__main__':
    rl = mp.get_context('spawn').RLock()
    with rl:
        with rl:  # повторный захват тем же владельцем
            print('nested ok')  # → nested ok
    try:
        rl.release()
    except AssertionError as e:
        print(e)  # → attempt to release recursive lock not owned by thread

See also

Open in the full glossarydocs.python.orgReport an inaccuracy