MCPcopy Create free account
hub / github.com/tox-dev/filelock / BaseFileLock

Class BaseFileLock

src/filelock/_api.py:182–612  ·  view source on GitHub ↗

Abstract base class for a file lock object. Provides a reentrant, cross-process exclusive lock backed by OS-level primitives. Subclasses implement the actual locking mechanism (:class:`UnixFileLock `, :class:`WindowsFileLock `, :clas

Source from the content-addressed store, hash-verified

180
181
182class BaseFileLock(contextlib.ContextDecorator, metaclass=FileLockMeta):
183 """
184 Abstract base class for a file lock object.
185
186 Provides a reentrant, cross-process exclusive lock backed by OS-level primitives. Subclasses implement the actual
187 locking mechanism (:class:`UnixFileLock <filelock.UnixFileLock>`, :class:`WindowsFileLock
188 <filelock.WindowsFileLock>`, :class:`SoftFileLock <filelock.SoftFileLock>`).
189
190 """
191
192 _instances: WeakValueDictionary[str, BaseFileLock]
193
194 def __init_subclass__(cls, **kwargs: dict[str, Any]) -> None:
195 """Setup unique state for lock subclasses."""
196 super().__init_subclass__(**kwargs)
197 cls._instances = WeakValueDictionary()
198
199 def __init__( # noqa: PLR0913
200 self,
201 lock_file: str | os.PathLike[str],
202 timeout: float = -1,
203 mode: int = _UNSET_FILE_MODE,
204 thread_local: bool = True, # noqa: FBT001, FBT002
205 *,
206 blocking: bool = True,
207 is_singleton: bool = False,
208 poll_interval: float = 0.05,
209 lifetime: float | None = None,
210 ) -> None:
211 """
212 Create a new lock object.
213
214 :param lock_file: path to the file
215 :param timeout: default timeout when acquiring the lock, in seconds. It will be used as fallback value in the
216 acquire method, if no timeout value (``None``) is given. If you want to disable the timeout, set it to a
217 negative value. A timeout of 0 means that there is exactly one attempt to acquire the file lock.
218 :param mode: file permissions for the lockfile. When not specified, the OS controls permissions via umask and
219 default ACLs, preserving POSIX default ACL inheritance in shared directories.
220 :param thread_local: Whether this object&#x27;s internal context should be thread local or not. If this is set to
221 ``False`` then the lock will be reentrant across threads. When ``True`` (the default), **all fields of the
222 lock&#x27;s internal context are per-thread**, including the configuration values ``poll_interval``, ``timeout``,
223 ``blocking``, ``mode``, and ``lifetime``. Setting one of these properties from one thread does not change
224 the value seen by another thread; threads that did not perform the write continue to see the value supplied
225 at construction time. If you need configuration values to be visible across threads, construct the lock
226 with ``thread_local=False``.
227 :param blocking: whether the lock should be blocking or not
228 :param is_singleton: If this is set to ``True`` then only one instance of this class will be created per lock
229 file. This is useful if you want to use the lock object for reentrant locking without needing to pass the
230 same object around.
231 :param poll_interval: default interval for polling the lock file, in seconds. It will be used as fallback value
232 in the acquire method, if no poll_interval value (``None``) is given.
233 :param lifetime: maximum time in seconds a lock can be held before it is considered expired. When set, a waiting
234 process will break a lock whose file modification time is older than ``lifetime`` seconds. ``None`` (the
235 default) means locks never expire.
236
237 """
238 self._is_thread_local = thread_local
239 self._is_singleton = is_singleton

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…