labthings_fastapi.global_lock

Global locking.

If the feature is enabled, a global lock is used to restrict running actions and setting properties. This module defines a wrapper for threading.RLock with a context manager that acquires the lock using a short timeout.

Classes

GlobalLock

An RLock wrapper and work-a-like with a default timeout.

Module Contents

class labthings_fastapi.global_lock.GlobalLock

An RLock wrapper and work-a-like with a default timeout.

Initialise the global lock.

_lock
default_timeout: float = 0.05
acquire(blocking: bool = True, timeout: float | types.EllipsisType = ...) bool

Acquire the lock.

This wraps the underlying threading.RLock.acquire but will by default block with a short timeout.

Parameters:
  • blocking – whether to wait for the lock to become free. True (the default) will block until the lock is available or we time out. False will always return immediately.

  • timeout – the length of time to wait for the lock, if blocking is True - or -1 to specify waiting forever.

Returns:

whether the lock was successfully acquired.

release() None

Release the lock.

This wraps threading.RLock.release without modification.

__enter__() None

Allow the lock to be used as a context manager.

The behaviour when used as a context manager is different from a regular threading.RLock because it will use the default timeout rather than blocking forever.

Raises:

GlobalLockBusyError – if the lock is in use by another thread.

__exit__(exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: types.TracebackType | None) None

Allow the lock to be used as a context manager.

The lock is released when the context ends. No error handling is done.

Parameters:
  • exc_type – the exception type, if one was raised (ignored).

  • exc_value – the exception, if one was raised (ignored).

  • traceback – the traceback, if an error was raised (ignored).