Create a write lock for a file. Args: file_path: Path to the file to lock timeout: Optional timeout in seconds operation: Description of the operation Returns: FileLock context manager Example: with write_lock(Path("data.json"), timeout=5.0
(
file_path: Path, timeout: float | None = None, operation: str = "write"
)
| 223 | |
| 224 | |
| 225 | def write_lock( |
| 226 | file_path: Path, timeout: float | None = None, operation: str = "write" |
| 227 | ) -> FileLock: |
| 228 | """ |
| 229 | Create a write lock for a file. |
| 230 | |
| 231 | Args: |
| 232 | file_path: Path to the file to lock |
| 233 | timeout: Optional timeout in seconds |
| 234 | operation: Description of the operation |
| 235 | |
| 236 | Returns: |
| 237 | FileLock context manager |
| 238 | |
| 239 | Example: |
| 240 | with write_lock(Path("data.json"), timeout=5.0) as lock: |
| 241 | # Write to file safely |
| 242 | with open("data.json", "w") as f: |
| 243 | json.dump(data, f) |
| 244 | """ |
| 245 | lock_file = file_path.parent / f".{file_path.name}.lock" |
| 246 | return FileLock(lock_file, timeout=timeout, operation=operation, mode="write") |
| 247 | |
| 248 | |
| 249 | def read_lock( |