lock
LockManagerLink
File-based locking for thread-safe resource management
The LockManager 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, fileset, or file 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
- Support for multiple lock levels: Scan, Fileset, and File
Usage ExamplesLink
from plantdb.commons.fsdb.lock import LockManager, LockType
# Initialize the lock manager with a base path
manager = LockManager('/path/to/database')
# Acquire an exclusive lock for a scan
with manager.acquire_lock('scan123', LockType.EXCLUSIVE, user='user1', level='scan'):
# Perform critical section operations...
# Acquire a shared lock for a fileset
with manager.acquire_lock('scan123/fileset1', LockType.SHARED, user='user1', level='fileset'):
# Perform critical section operations...
# Acquire an exclusive lock for a file
with manager.acquire_lock('scan123/fileset1/file1', LockType.EXCLUSIVE, user='user1', level='file'):
# Perform critical section operations...
LockError
Link
LockError(message)
Bases: Exception
Raised when the lock acquisition fails.
Source code in plantdb/commons/fsdb/lock.py
71 72 73 | |
LockLevel
Link
Bases: Enum
Enumeration of lock levels supported by the LockManager.
-
Reference API
commons
commons
fsdb
lock
LockManager
LockManager
Link
LockManager(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 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 LockManager
>>> manager = LockManager('/path/to/local/database')
>>> lock = manager.acquire_lock('scan123', LockType.EXCLUSIVE, user='user1', level='scan')
Lock manager constructor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str or Path
|
The base directory of the database. |
required |
|
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
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | |
acquire_lock
Link
acquire_lock(resource_id, lock_type, user, level, timeout=None)
Acquire a lock for a specific resource and lock type.
This method attempts to acquire a lock (either shared or exclusive) for a given resource 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 |
|---|---|---|---|
|
str
|
The unique identifier for the resource (scan_id, scan_id/fileset_id, or scan_id/fileset_id/file_id). |
required |
|
LockType
|
The type of lock to acquire (shared or exclusive). |
required |
|
str
|
The user requesting the lock. |
required |
|
LockLevel
|
The level of the lock (scan, fileset, or file). |
required |
|
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.
Examples:
>>> import tempfile
>>> from plantdb.commons.fsdb.lock import LockManager
>>> from plantdb.commons.fsdb.lock import LockType
>>> from plantdb.commons.fsdb.lock import LockLevel
>>> manager = LockManager(tempfile.gettempdir())
>>> with manager.acquire_lock('scan123', LockType.EXCLUSIVE, user='user1', level=LockLevel.SCAN):
... print("scan123/scan/exclusive" in manager._active_locks)
True
>>> with manager.acquire_lock('scan123/test_fileset', LockType.EXCLUSIVE, user='user1', level=LockLevel.FILESET):
... print("scan123/test_fileset/fileset/exclusive" in manager._active_locks)
True
Source code in plantdb/commons/fsdb/lock.py
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 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 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 480 481 482 483 484 485 486 487 488 489 490 491 | |
cleanup_all_locks
Link
cleanup_all_locks()
Emergency cleanup of all locks (use with caution)
Source code in plantdb/commons/fsdb/lock.py
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 | |
get_lock_status
Link
get_lock_status(resource_id, level)
Retrieve the current lock status for a given resource ID.
This method checks all active locks and determines if there is an exclusive or shared lock associated with the specified resource. It returns a dictionary containing information about the exclusive lock (if any) and all shared locks associated with the resource.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
The unique identifier of the resource for which to retrieve the lock status. |
required |
|
LockLevel
|
The level of the lock (scan, fileset, or file). |
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 resource ID. It distinguishes between exclusive and shared locks based on their type.
Examples:
>>> import tempfile
>>> from plantdb.commons.fsdb.lock import LockManager
>>> from plantdb.commons.fsdb.lock import LockType
>>> from plantdb.commons.fsdb.lock import LockLevel
>>> manager = LockManager(tempfile.gettempdir())
>>> with manager.acquire_lock('scan123', LockType.EXCLUSIVE, user='user1', level=LockLevel.SCAN):
... status = manager.get_lock_status("scan123", LockLevel.SCAN)
... print(status)
{'exclusive': {'user': 'user1', 'timestamp': 1775734565.6584513}, 'shared': []}
>>> with manager.acquire_lock('scan123/test_fileset', LockType.EXCLUSIVE, user='user1', level=LockLevel.FILESET):
... status = manager.get_lock_status("scan123/test_fileset", LockLevel.FILESET)
... print(status)
{'exclusive': {'user': 'user1', 'timestamp': 1775734690.2060757}, 'shared': []}
Source code in plantdb/commons/fsdb/lock.py
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 | |
LockTimeoutError
Link
LockTimeoutError(message='Error: Lock acquisition timed-out!')
Bases: Exception
Raised when the lock acquisition times out
-
Reference API
commons
commons
fsdb
lock
LockManageracquire_lock
Source code in plantdb/commons/fsdb/lock.py
82 83 84 | |