_filelock.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. from types import TracebackType
  2. from typing import Optional
  3. from typing_extensions import Self
  4. from filelock import FileLock as base_FileLock
  5. from torch.monitor import _WaitCounter
  6. class FileLock(base_FileLock):
  7. """
  8. This behaves like a normal file lock.
  9. However, it adds waitcounters for acquiring and releasing the filelock
  10. as well as for the critical region within it.
  11. pytorch.filelock.enter - While we're acquiring the filelock.
  12. pytorch.filelock.region - While we're holding the filelock and doing work.
  13. pytorch.filelock.exit - While we're releasing the filelock.
  14. """
  15. def __enter__(self) -> Self:
  16. self.region_counter = _WaitCounter("pytorch.filelock.region").guard()
  17. with _WaitCounter("pytorch.filelock.enter").guard():
  18. result = super().__enter__()
  19. self.region_counter.__enter__()
  20. return result
  21. def __exit__(
  22. self,
  23. exc_type: Optional[type[BaseException]],
  24. exc_value: Optional[BaseException],
  25. traceback: Optional[TracebackType],
  26. ) -> None:
  27. self.region_counter.__exit__()
  28. with _WaitCounter("pytorch.filelock.exit").guard():
  29. # Returns nothing per
  30. # https://github.com/tox-dev/filelock/blob/57f488ff8fdc2193572efe102408fb63cfefe4e4/src/filelock/_api.py#L379
  31. super().__exit__(exc_type, exc_value, traceback)
  32. # Returns nothing per
  33. # https://github.com/pytorch/pytorch/blob/0f6bfc58a2cfb7a5c052bea618ab62becaf5c912/torch/csrc/monitor/python_init.cpp#L315
  34. return None