filelock.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import hashlib
  2. import os
  3. from pathlib import Path
  4. from filelock import FileLock
  5. import ray
  6. RAY_LOCKFILE_DIR = "_ray_lockfiles"
  7. class TempFileLock:
  8. """FileLock wrapper that uses temporary file locks.
  9. The temporary directory that these locks are saved to can be configured via
  10. the `RAY_TMPDIR` environment variable.
  11. Args:
  12. path: The file path that this temporary file lock is used for.
  13. This will be used to generate the lockfile filename.
  14. Ex: For concurrent writes to a file, this is the common filepath
  15. that multiple processes are writing to.
  16. **kwargs: Additional keyword arguments to pass to the underlying `FileLock`.
  17. """
  18. def __init__(self, path: str, **kwargs):
  19. self.path = path
  20. temp_dir = Path(ray._common.utils.get_user_temp_dir()).resolve()
  21. self._lock_dir = temp_dir / RAY_LOCKFILE_DIR
  22. self._path_hash = hashlib.sha256(
  23. str(Path(self.path).resolve()).encode("utf-8")
  24. ).hexdigest()
  25. self._lock_path = self._lock_dir / f"{self._path_hash}.lock"
  26. os.makedirs(str(self._lock_dir), exist_ok=True)
  27. self._lock = FileLock(self._lock_path, **kwargs)
  28. def __enter__(self):
  29. self._lock.acquire()
  30. return self
  31. def __exit__(self, type, value, traceback):
  32. self._lock.release()
  33. def __getattr__(self, name):
  34. return getattr(self._lock, name)