lock
plantdb.commons.fsdb.lock Link
ScanLockManagerLink
File-based locking for thread-safe resource management
The ScanLockManager
module provides a robust file-based locking mechanism to ensure thread-safe operations across multiple threads or processes.
It allows acquiring and releasing locks on resources identified by scan IDs, with support for both shared (read) and exclusive (write) locks.
Key FeaturesLink
- Thread-safe lock acquisition and release using file-based locking mechanisms
- Support for both shared (multiple readers) and exclusive (single writer) locks
- Lock timeout to prevent indefinite waiting for lock acquisition
- Automatic cleanup of stale locks on initialization
- Monitoring and debugging capabilities with lock metadata storage
Usage ExamplesLink
from plantdb.commons.fsdb.lock import ScanLockManager
# Initialize the lock manager with a base path
manager = ScanLockManager('/path/to/database')
# Acquire an exclusive lock for 'scan123'
with manager.acquire_lock('scan123', LockType.EXCLUSIVE, user='user1'):
# Perform critical section operations...
# Check the status of locks for 'scan123'
status = manager.get_lock_status('scan123')
print(status)
LockError Link
LockError(message)
Bases: Exception
Raised when lock acquisition fails.
Source code in plantdb/commons/fsdb/lock.py
59 60 61 |
|
LockTimeoutError Link
LockTimeoutError(message='Error: Lock acquisition timed-out!')
Bases: Exception
Raised when lock acquisition times out
Source code in plantdb/commons/fsdb/lock.py
70 71 72 |
|
ScanLockManager Link
ScanLockManager(base_path, default_timeout=30.0, **kwargs)
Acquires and releases file-based locks for thread-safe resource management.
This class provides functionality for acquiring and releasing file-based locks, ensuring thread-safe operations across multiple threads or processes. Locks are stored in a designated subdirectory within the specified base path, which is created if it does not exist. On initialization, stale lock files are cleaned up to maintain consistency.
The class leverages thread locks to manage access to shared resources and uses file-based locking mechanisms to ensure exclusive access across processes.
Attributes:
Name | Type | Description |
---|---|---|
base_path |
str
|
The base directory of the database. |
default_timeout |
float
|
The default timeout duration in seconds when attempting to acquire a lock. Default is 30.0 seconds. |
locks_dir |
str
|
Location of the directory where lock files are stored. |
_active_locks |
Dict[str, Dict]
|
Dictionary mapping lock names (scan ID + lock type) to dictionaries containing information about the lock including:
|
_lock_files |
Dict[str, int]
|
The file descriptors (int) for each active lock in the dictionary mapping. |
_thread_lock |
RLock
|
A thread-safe lock used to synchronize access to the active locks dictionary and file descriptors. |
Examples:
>>> from plantdb.commons.fsdb.lock import ScanLockManager
>>> manager = ScanLockManager('/path/to/local/database')
>>> lock = manager.acquire_lock('scan123')
Lock manager constructor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
base_path
|
str or Path
|
The base directory of the database. |
required |
default_timeout
|
float
|
The default timeout duration in seconds when attempting to acquire a lock. Default is 30.0 seconds. |
30.0
|
Other Parameters:
Name | Type | Description |
---|---|---|
log_level |
str
|
The log level (e.g., |
Source code in plantdb/commons/fsdb/lock.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
|
acquire_lock Link
acquire_lock(scan_id, lock_type, user, timeout=None)
Acquire a lock for a specific scan and lock type.
This method attempts to acquire a lock (either shared or exclusive) for a given scan ID. If the lock is successfully acquired, it yields control to the calling code. Upon completion of the calling code, it ensures that the lock is released.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scan_id
|
str
|
The unique identifier for the scan. |
required |
lock_type
|
LockType
|
The type of lock to acquire (shared or exclusive). |
required |
user
|
str
|
The user requesting the lock. |
required |
timeout
|
Optional[float]
|
The maximum time to wait for acquiring the lock, in seconds. If not provided, uses a default timeout value. |
None
|
Raises:
Type | Description |
---|---|
LockTimeoutError
|
If the lock could not be acquired within the specified timeout period or if an exclusive lock is already held by another user. |
OSError
|
If there is an error with file operations while trying to acquire the lock. |
IOError
|
If there is an input/output error while trying to acquire the lock. |
Notes
This method uses a context manager and should be used within a 'with' statement. It handles both shared and exclusive locks, allowing multiple acquisitions for shared locks but raising an exception if an exclusive lock is already held.
Source code in plantdb/commons/fsdb/lock.py
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
|
cleanup_all_locks Link
cleanup_all_locks()
Emergency cleanup of all locks (use with caution)
Source code in plantdb/commons/fsdb/lock.py
481 482 483 484 485 486 487 488 489 |
|
get_lock_status Link
get_lock_status(scan_id)
Retrieve the current lock status for a given scan ID.
This method checks all active locks and determines if there is an exclusive or shared lock associated with the specified scan ID. It returns a dictionary containing information about the exclusive lock (if any) and all shared locks associated with the scan ID.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scan_id
|
str
|
The unique identifier of the scan for which to retrieve the lock status. |
required |
Returns:
Name | Type | Description |
---|---|---|
status |
dict
|
A dictionary with two keys: - 'exclusive': Information about the exclusive lock (if any), or None. This is a nested dictionary containing the user and timestamp. - 'shared': A list of dictionaries, each representing a shared lock. Each dictionary contains the user, timestamp, and count. |
Notes
This method iterates through all active locks to find matches with the given scan ID. It distinguishes between exclusive and shared locks based on their type.
Examples:
>>> from plantdb.commons.fsdb.lock import ScanLockManager
>>> manager = ScanLockManager('/path/to/local/database')
>>> lock = manager.acquire_lock('scan123')
>>> status = manager.get_lock_status("scan123")
>>> print(status)
{'exclusive': None, 'shared': [{'user': 'user1', 'timestamp': '2023-01-01T12:00:00', 'count': 1}]}
Source code in plantdb/commons/fsdb/lock.py
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 |
|