Skip to content

Process Lock API

InterProcessLock

An interprocess lock.

Functions

__init__(path, sleep_func=time.sleep, logger=None)

Parameters:

Name Type Description Default
path Union[Path, str]

Path to the file that will be used for locking.

required
sleep_func Callable[[float], None]

Optional function to use for sleeping.

sleep
logger Optional[logging.Logger]

Optional logger to use for logging.

None

acquire(blocking=True, delay=0.01, max_delay=0.1, timeout=None)

Attempt to acquire the lock.

Parameters:

Name Type Description Default
blocking bool

Whether to wait to try to acquire the lock.

True
delay float

When blocking, starting delay as well as the delay increment (in seconds).

0.01
max_delay float

When blocking the maximum delay in between attempts to acquire (in seconds).

0.1
timeout Optional[float]

When blocking, maximal waiting time (in seconds).

None

Returns:

Type Description
bool

whether or not the acquisition succeeded

release()

Release the previously acquired lock.

InterProcessReaderWriterLock

An interprocess readers writer lock.

Functions

__init__(path, sleep_func=time.sleep, logger=None)

Parameters:

Name Type Description Default
path Union[Path, str]

Path to the file that will be used for locking.

required
sleep_func Callable[[float], None]

Optional function to use for sleeping.

sleep
logger Optional[logging.Logger]

Optional logger to use for logging.

None

read_lock(delay=0.01, max_delay=0.1)

Context manager that grans a read lock

write_lock(delay=0.01, max_delay=0.1)

Context manager that grans a write lock

acquire_read_lock(blocking=True, delay=0.01, max_delay=0.1, timeout=None)

Attempt to acquire a reader's lock.

Parameters:

Name Type Description Default
blocking bool

Whether to wait to try to acquire the lock.

True
delay float

When blocking, starting delay as well as the delay increment (in seconds).

0.01
max_delay float

When blocking the maximum delay in between attempts to acquire (in seconds).

0.1
timeout float

When blocking, maximal waiting time (in seconds).

None

Returns:

Type Description
bool

whether or not the acquisition succeeded

acquire_write_lock(blocking=True, delay=0.01, max_delay=0.1, timeout=None)

Attempt to acquire a writer's lock.

Parameters:

Name Type Description Default
blocking bool

Whether to wait to try to acquire the lock.

True
delay float

When blocking, starting delay as well as the delay increment (in seconds).

0.01
max_delay float

When blocking the maximum delay in between attempts to acquire (in seconds).

0.1
timeout float

When blocking, maximal waiting time (in seconds).

None

Returns:

Type Description
bool

whether or not the acquisition succeeded

release_write_lock()

Release the writer's lock.

release_read_lock()

Release the reader's lock.

Decorators

interprocess_locked(path)

Acquires & releases an interprocess lock around the call to the decorated function.

Parameters:

Name Type Description Default
path Union[Path, str]

Path to the file used for locking.

required

interprocess_read_locked(path)

Acquires & releases an interprocess read lock around the call into the decorated function

Parameters:

Name Type Description Default
path Union[Path, str]

Path to the file used for locking.

required

interprocess_write_locked(path)

Acquires & releases an interprocess write lock around the call into the decorated function

Parameters:

Name Type Description Default
path Union[Path, str]

Path to the file used for locking.

required