all Python 3threadingterm
threading.RLock
synchronization
A reentrant mutex — one thread can acquire it several times without blocking itself. Use it instead of Lock with recursive calls.
Syntax
rlock = threading.RLock()
with rlock: ...
It tracks acquisition depth: a thread must call release() as many times as it called acquire() before the lock frees up — a missed release leaves it held, and only the owning thread can release it. RLock does nothing against deadlock between different threads: it only cures re-acquisition within one thread and costs a bit more than a plain Lock, so without real reentrancy use Lock.
Examples
import threading
rlock = threading.RLock()
def recursive(n):
with rlock:
if n > 0:
recursive(n - 1) # Lock здесь бы заблокировал
print(n)
recursive(3)
See also
Open in the full glossarydocs.python.orgReport an inaccuracy