_api.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. from __future__ import annotations
  2. import contextlib
  3. import logging
  4. import os
  5. import time
  6. import warnings
  7. from abc import ABC, abstractmethod
  8. from dataclasses import dataclass
  9. from threading import local
  10. from typing import TYPE_CHECKING, Any
  11. from ._error import Timeout
  12. if TYPE_CHECKING:
  13. from types import TracebackType
  14. _LOGGER = logging.getLogger("filelock")
  15. # This is a helper class which is returned by :meth:`BaseFileLock.acquire` and wraps the lock to make sure __enter__
  16. # is not called twice when entering the with statement. If we would simply return *self*, the lock would be acquired
  17. # again in the *__enter__* method of the BaseFileLock, but not released again automatically. issue #37 (memory leak)
  18. class AcquireReturnProxy:
  19. """A context aware object that will release the lock file when exiting."""
  20. def __init__(self, lock: BaseFileLock) -> None:
  21. self.lock = lock
  22. def __enter__(self) -> BaseFileLock:
  23. return self.lock
  24. def __exit__(
  25. self,
  26. exc_type: type[BaseException] | None,
  27. exc_value: BaseException | None,
  28. traceback: TracebackType | None,
  29. ) -> None:
  30. self.lock.release()
  31. @dataclass
  32. class FileLockContext:
  33. """A dataclass which holds the context for a ``BaseFileLock`` object."""
  34. # The context is held in a separate class to allow optional use of thread local storage via the
  35. # ThreadLocalFileContext class.
  36. #: The path to the lock file.
  37. lock_file: str
  38. #: The default timeout value.
  39. timeout: float
  40. #: The mode for the lock files
  41. mode: int
  42. #: The file descriptor for the *_lock_file* as it is returned by the os.open() function, not None when lock held
  43. lock_file_fd: int | None = None
  44. #: The lock counter is used for implementing the nested locking mechanism.
  45. lock_counter: int = 0 # When the lock is acquired is increased and the lock is only released, when this value is 0
  46. class ThreadLocalFileContext(FileLockContext, local):
  47. """A thread local version of the ``FileLockContext`` class."""
  48. class BaseFileLock(ABC, contextlib.ContextDecorator):
  49. """Abstract base class for a file lock object."""
  50. def __init__(
  51. self,
  52. lock_file: str | os.PathLike[Any],
  53. timeout: float = -1,
  54. mode: int = 0o644,
  55. thread_local: bool = True, # noqa: FBT001, FBT002
  56. ) -> None:
  57. """
  58. Create a new lock object.
  59. :param lock_file: path to the file
  60. :param timeout: default timeout when acquiring the lock, in seconds. It will be used as fallback value in
  61. the acquire method, if no timeout value (``None``) is given. If you want to disable the timeout, set it
  62. to a negative value. A timeout of 0 means, that there is exactly one attempt to acquire the file lock.
  63. :param mode: file permissions for the lockfile.
  64. :param thread_local: Whether this object's internal context should be thread local or not.
  65. If this is set to ``False`` then the lock will be reentrant across threads.
  66. """
  67. self._is_thread_local = thread_local
  68. # Create the context. Note that external code should not work with the context directly and should instead use
  69. # properties of this class.
  70. kwargs: dict[str, Any] = {
  71. "lock_file": os.fspath(lock_file),
  72. "timeout": timeout,
  73. "mode": mode,
  74. }
  75. self._context: FileLockContext = (ThreadLocalFileContext if thread_local else FileLockContext)(**kwargs)
  76. def is_thread_local(self) -> bool:
  77. """:return: a flag indicating if this lock is thread local or not"""
  78. return self._is_thread_local
  79. @property
  80. def lock_file(self) -> str:
  81. """:return: path to the lock file"""
  82. return self._context.lock_file
  83. @property
  84. def timeout(self) -> float:
  85. """
  86. :return: the default timeout value, in seconds
  87. .. versionadded:: 2.0.0
  88. """
  89. return self._context.timeout
  90. @timeout.setter
  91. def timeout(self, value: float | str) -> None:
  92. """
  93. Change the default timeout value.
  94. :param value: the new value, in seconds
  95. """
  96. self._context.timeout = float(value)
  97. @abstractmethod
  98. def _acquire(self) -> None:
  99. """If the file lock could be acquired, self._context.lock_file_fd holds the file descriptor of the lock file."""
  100. raise NotImplementedError
  101. @abstractmethod
  102. def _release(self) -> None:
  103. """Releases the lock and sets self._context.lock_file_fd to None."""
  104. raise NotImplementedError
  105. @property
  106. def is_locked(self) -> bool:
  107. """
  108. :return: A boolean indicating if the lock file is holding the lock currently.
  109. .. versionchanged:: 2.0.0
  110. This was previously a method and is now a property.
  111. """
  112. return self._context.lock_file_fd is not None
  113. @property
  114. def lock_counter(self) -> int:
  115. """:return: The number of times this lock has been acquired (but not yet released)."""
  116. return self._context.lock_counter
  117. def acquire(
  118. self,
  119. timeout: float | None = None,
  120. poll_interval: float = 0.05,
  121. *,
  122. poll_intervall: float | None = None,
  123. blocking: bool = True,
  124. ) -> AcquireReturnProxy:
  125. """
  126. Try to acquire the file lock.
  127. :param timeout: maximum wait time for acquiring the lock, ``None`` means use the default :attr:`~timeout` is and
  128. if ``timeout < 0``, there is no timeout and this method will block until the lock could be acquired
  129. :param poll_interval: interval of trying to acquire the lock file
  130. :param poll_intervall: deprecated, kept for backwards compatibility, use ``poll_interval`` instead
  131. :param blocking: defaults to True. If False, function will return immediately if it cannot obtain a lock on the
  132. first attempt. Otherwise, this method will block until the timeout expires or the lock is acquired.
  133. :raises Timeout: if fails to acquire lock within the timeout period
  134. :return: a context object that will unlock the file when the context is exited
  135. .. code-block:: python
  136. # You can use this method in the context manager (recommended)
  137. with lock.acquire():
  138. pass
  139. # Or use an equivalent try-finally construct:
  140. lock.acquire()
  141. try:
  142. pass
  143. finally:
  144. lock.release()
  145. .. versionchanged:: 2.0.0
  146. This method returns now a *proxy* object instead of *self*,
  147. so that it can be used in a with statement without side effects.
  148. """
  149. # Use the default timeout, if no timeout is provided.
  150. if timeout is None:
  151. timeout = self._context.timeout
  152. if poll_intervall is not None:
  153. msg = "use poll_interval instead of poll_intervall"
  154. warnings.warn(msg, DeprecationWarning, stacklevel=2)
  155. poll_interval = poll_intervall
  156. # Increment the number right at the beginning. We can still undo it, if something fails.
  157. self._context.lock_counter += 1
  158. lock_id = id(self)
  159. lock_filename = self.lock_file
  160. start_time = time.perf_counter()
  161. try:
  162. while True:
  163. if not self.is_locked:
  164. _LOGGER.debug("Attempting to acquire lock %s on %s", lock_id, lock_filename)
  165. self._acquire()
  166. if self.is_locked:
  167. _LOGGER.debug("Lock %s acquired on %s", lock_id, lock_filename)
  168. break
  169. if blocking is False:
  170. _LOGGER.debug("Failed to immediately acquire lock %s on %s", lock_id, lock_filename)
  171. raise Timeout(lock_filename) # noqa: TRY301
  172. if 0 <= timeout < time.perf_counter() - start_time:
  173. _LOGGER.debug("Timeout on acquiring lock %s on %s", lock_id, lock_filename)
  174. raise Timeout(lock_filename) # noqa: TRY301
  175. msg = "Lock %s not acquired on %s, waiting %s seconds ..."
  176. _LOGGER.debug(msg, lock_id, lock_filename, poll_interval)
  177. time.sleep(poll_interval)
  178. except BaseException: # Something did go wrong, so decrement the counter.
  179. self._context.lock_counter = max(0, self._context.lock_counter - 1)
  180. raise
  181. return AcquireReturnProxy(lock=self)
  182. def release(self, force: bool = False) -> None: # noqa: FBT001, FBT002
  183. """
  184. Releases the file lock. Please note, that the lock is only completely released, if the lock counter is 0. Also
  185. note, that the lock file itself is not automatically deleted.
  186. :param force: If true, the lock counter is ignored and the lock is released in every case/
  187. """
  188. if self.is_locked:
  189. self._context.lock_counter -= 1
  190. if self._context.lock_counter == 0 or force:
  191. lock_id, lock_filename = id(self), self.lock_file
  192. _LOGGER.debug("Attempting to release lock %s on %s", lock_id, lock_filename)
  193. self._release()
  194. self._context.lock_counter = 0
  195. _LOGGER.debug("Lock %s released on %s", lock_id, lock_filename)
  196. def __enter__(self) -> BaseFileLock:
  197. """
  198. Acquire the lock.
  199. :return: the lock object
  200. """
  201. self.acquire()
  202. return self
  203. def __exit__(
  204. self,
  205. exc_type: type[BaseException] | None,
  206. exc_value: BaseException | None,
  207. traceback: TracebackType | None,
  208. ) -> None:
  209. """
  210. Release the lock.
  211. :param exc_type: the exception type if raised
  212. :param exc_value: the exception value if raised
  213. :param traceback: the exception traceback if raised
  214. """
  215. self.release()
  216. def __del__(self) -> None:
  217. """Called when the lock object is deleted."""
  218. self.release(force=True)
  219. __all__ = [
  220. "BaseFileLock",
  221. "AcquireReturnProxy",
  222. ]