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

Class BaseAsyncFileLock

src/filelock/asyncio.py:117–690  ·  view source on GitHub ↗

Base class for asynchronous file locks. .. versionadded:: 3.15.0

Source from the content-addressed store, hash-verified

115
116
117class BaseAsyncFileLock(BaseFileLock, metaclass=AsyncFileLockMeta):
118 """
119 Base class for asynchronous file locks.
120
121 .. versionadded:: 3.15.0
122
123 """
124
125 _deadlock_holder_desc: str = "BaseAsyncFileLock instance in this task"
126 _constructor_lifetime_warning_stacklevel: int = 4
127
128 @staticmethod
129 def _deadlock_scope() -> Hashable | None:
130 # One event loop thread runs every task, so a thread-scoped registry cannot tell a same-task reacquire
131 # (a real deadlock: the polling task never reaches its own release) from another task queuing behind the
132 # holder (no deadlock: each poll yields, so the holder runs on and releases). Only the first may fail
133 # fast, so scope holders to the task.
134 return asyncio.current_task()
135
136 def __init__( # ruff:ignore[too-many-arguments] # public constructor: one parameter per documented lock option
137 self,
138 lock_file: str | os.PathLike[str],
139 timeout: float = -1,
140 mode: int = _UNSET_FILE_MODE,
141 thread_local: bool = False, # ruff:ignore[boolean-type-hint-positional-argument, boolean-default-value-positional-argument] # public API: positional bool kept for backwards compatibility
142 *,
143 blocking: bool = True,
144 is_singleton: bool = False,
145 poll_interval: float = 0.05,
146 lifetime: float | None = None,
147 context_error_policy: ContextErrorPolicy = "chain",
148 close_error_policy: CloseErrorPolicy = "default",
149 fallback_to_soft: bool = True,
150 preserve_lock_file: bool = False,
151 on_acquired: Callable[[int], None] | None = None,
152 loop: asyncio.AbstractEventLoop | None = None,
153 run_in_executor: bool = True,
154 executor: futures.Executor | None = None,
155 ) -> None:
156 """
157 Create a new lock object.
158
159 :param lock_file: path to the file
160 :param timeout: default timeout when acquiring the lock, in seconds. It will be used as fallback value in the
161 acquire method, if no timeout value (``None``) is given. If you want to disable the timeout, set it to a
162 negative value. A timeout of 0 means that there is exactly one attempt to acquire the file lock.
163 :param mode: file permissions for the lockfile. When not specified, the OS controls permissions via umask and
164 default ACLs, preserving POSIX default ACL inheritance in shared directories.
165 :param thread_local: Whether this object's internal context should be thread local or not. If this is set to
166 ``False`` then the lock will be reentrant across threads. When ``True`` (the default), **all fields of the
167 lock's internal context are per-thread**, including the configuration values ``poll_interval``, ``timeout``,
168 ``blocking``, ``mode``, and ``lifetime``. Setting one of these properties from one thread does not change
169 the value seen by another thread; threads that did not perform the write continue to see the value supplied
170 at construction time. If you need configuration values to be visible across threads, construct the lock
171 with ``thread_local=False``.
172 :param blocking: whether the lock should be blocking or not
173 :param is_singleton: If this is set to ``True`` then only one instance of this class will be created per lock
174 file. This is useful if you want to use the lock object for reentrant locking without needing to pass the

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected