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
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
- acquire(blocking: bool = True, timeout: float | types.EllipsisType = ...) bool
Acquire the lock.
This wraps the underlying
threading.RLock.acquirebut will by default block with a short timeout.- Parameters:
- Returns:
whether the lock was successfully acquired.
- release() None
Release the lock.
This wraps
threading.RLock.releasewithout 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.RLockbecause 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).