Create a read lock for a file. Allows multiple concurrent readers. Blocks only if a writer holds the lock. Args: file_path: Path to the file to lock timeout: Optional timeout in seconds operation: Description of the operation Returns: FileLock cont
(
file_path: Path, timeout: float | None = None, operation: str = "read"
)
| 247 | |
| 248 | |
| 249 | def read_lock( |
| 250 | file_path: Path, timeout: float | None = None, operation: str = "read" |
| 251 | ) -> FileLock: |
| 252 | """ |
| 253 | Create a read lock for a file. |
| 254 | |
| 255 | Allows multiple concurrent readers. Blocks only if a writer holds the lock. |
| 256 | |
| 257 | Args: |
| 258 | file_path: Path to the file to lock |
| 259 | timeout: Optional timeout in seconds |
| 260 | operation: Description of the operation |
| 261 | |
| 262 | Returns: |
| 263 | FileLock context manager |
| 264 | |
| 265 | Example: |
| 266 | with read_lock(Path("data.json"), timeout=5.0) as lock: |
| 267 | # Read from file safely |
| 268 | with open("data.json", "r") as f: |
| 269 | data = json.load(f) |
| 270 | """ |
| 271 | lock_file = file_path.parent / f".{file_path.name}.lock" |
| 272 | return FileLock(lock_file, timeout=timeout, operation=operation, mode="read") |
| 273 | |
| 274 | |
| 275 | def custom_lock( |