Lock
provide.foundation.file.lock
¶
TODO: Add module docstring.
Classes¶
FileLock
¶
FileLock(
path: Path | str,
timeout: float = DEFAULT_FILE_LOCK_TIMEOUT,
check_interval: float = 0.1,
)
File-based lock for concurrent access control.
Uses exclusive file creation as the locking mechanism. The lock file contains the PID of the process holding the lock.
Thread-safe: Multiple threads can safely use the same FileLock instance. The internal thread lock protects instance state while the file lock provides inter-process synchronization.
Example
with FileLock("/tmp/myapp.lock"): # Exclusive access to resource do_something()
Initialize file lock.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
Path | str
|
Lock file path |
required |
timeout
|
float
|
Max seconds to wait for lock |
DEFAULT_FILE_LOCK_TIMEOUT
|
check_interval
|
float
|
Seconds between lock checks |
0.1
|
Source code in provide/foundation/file/lock.py
Functions¶
__enter__
¶
__exit__
¶
acquire
¶
Acquire the lock.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
blocking
|
bool
|
If True, wait for lock. If False, return immediately. |
True
|
Returns:
| Type | Description |
|---|---|
bool
|
True if lock acquired, False if not (non-blocking mode only) |
Raises:
| Type | Description |
|---|---|
LockError
|
If timeout exceeded (blocking mode) |
Source code in provide/foundation/file/lock.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 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 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 | |
release
¶
Release the lock.
Only removes the lock file if we own it.
Source code in provide/foundation/file/lock.py
LockError
¶
LockError(
message: str,
*,
lock_path: str | None = None,
timeout: float | None = None,
**kwargs: Any
)
Bases: FoundationError
Raised when file lock operations fail.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Error message describing the lock issue. |
required |
lock_path
|
str | None
|
Optional path to the lock file. |
None
|
timeout
|
float | None
|
Optional timeout that was exceeded. |
None
|
**kwargs
|
Any
|
Additional context passed to FoundationError. |
{}
|
Examples:
>>> raise LockError("Failed to acquire lock")
>>> raise LockError("Lock timeout", lock_path="/tmp/app.lock", timeout=30)