Warning

You are reading the documentation for a development version. The latest release is v0.0.15.

Usage

Installation

To use dreadlocks, first install it using pip:

(.venv) $ pip install dreadlocks

Using dreadlocks

Public API members are dreadlocks.path_lock(), dreadlocks.process_level_path_lock(), dreadlocks.thread_level_path_lock(), dreadlocks.AcquiringLockWouldBlockError, dreadlocks.AcquiringProcessLevelLockWouldBlockError, dreadlocks.AcquiringThreadLevelLockWouldBlockError, dreadlocks.RecursiveDeadlockError.

>>> from dreadlocks import path_lock, ...

To blockingly acquire an exclusive non-reentrant global (thread-level and process-level) lock on file named .lock, use dreadlocks.path_lock() as follows:

>>> with path_lock('.lock'):
>>>   ...

Note that attempting to acquire a non-reentrant lock may raise dreadlocks.RecursiveDeadlockError.

Exclusivity, blockingness, and reentrancy are all configurable through flags:

>>> with path_lock('.lock', shared=True, blocking=False, reentrant=True):
>>>   ...

Note that attempting to acquire a lock non-blockingly may raise dreadlocks.AcquiringLockWouldBlockError.

dreadlocks.process_level_path_lock() and dreadlocks.thread_level_path_lock() have similar APIs. In fact, dreadlocks.path_lock() is made out of the composition of those two lower-level constructs. One notable difference is that dreadlocks.thread_level_path_lock() will not create a lock file.