from __future__ import annotations from contextlib import contextmanager from pathlib import Path import fcntl @contextmanager def exclusive(db_path: Path): """Cross-process lock for destructive dedupe operations.""" lock_path = Path(str(db_path) + ".lock") lock_path.parent.mkdir(parents=True, exist_ok=True) with lock_path.open("w") as handle: fcntl.flock(handle.fileno(), fcntl.LOCK_EX) try: yield finally: fcntl.flock(handle.fileno(), fcntl.LOCK_UN)