Base class for asynchronous file locks.
| 99 | |
| 100 | |
| 101 | class BaseAsyncFileLock(BaseFileLock, metaclass=AsyncFileLockMeta): |
| 102 | """Base class for asynchronous file locks.""" |
| 103 | |
| 104 | def __init__( # noqa: PLR0913 |
| 105 | self, |
| 106 | lock_file: str | os.PathLike[str], |
| 107 | timeout: float = -1, |
| 108 | mode: int = 0o644, |
| 109 | thread_local: bool = False, # noqa: FBT001, FBT002 |
| 110 | *, |
| 111 | blocking: bool = True, |
| 112 | is_singleton: bool = False, |
| 113 | loop: asyncio.AbstractEventLoop | None = None, |
| 114 | run_in_executor: bool = True, |
| 115 | executor: futures.Executor | None = None, |
| 116 | ) -> None: |
| 117 | """ |
| 118 | Create a new lock object. |
| 119 | |
| 120 | :param lock_file: path to the file |
| 121 | :param timeout: default timeout when acquiring the lock, in seconds. It will be used as fallback value in \ |
| 122 | the acquire method, if no timeout value (``None``) is given. If you want to disable the timeout, set it \ |
| 123 | to a negative value. A timeout of 0 means that there is exactly one attempt to acquire the file lock. |
| 124 | :param mode: file permissions for the lockfile |
| 125 | :param thread_local: Whether this object's internal context should be thread local or not. If this is set to \ |
| 126 | ``False`` then the lock will be reentrant across threads. |
| 127 | :param blocking: whether the lock should be blocking or not |
| 128 | :param is_singleton: If this is set to ``True`` then only one instance of this class will be created \ |
| 129 | per lock file. This is useful if you want to use the lock object for reentrant locking without needing \ |
| 130 | to pass the same object around. |
| 131 | :param loop: The event loop to use. If not specified, the running event loop will be used. |
| 132 | :param run_in_executor: If this is set to ``True`` then the lock will be acquired in an executor. |
| 133 | :param executor: The executor to use. If not specified, the default executor will be used. |
| 134 | |
| 135 | """ |
| 136 | self._is_thread_local = thread_local |
| 137 | self._is_singleton = is_singleton |
| 138 | |
| 139 | # Create the context. Note that external code should not work with the context directly and should instead use |
| 140 | # properties of this class. |
| 141 | kwargs: dict[str, Any] = { |
| 142 | "lock_file": os.fspath(lock_file), |
| 143 | "timeout": timeout, |
| 144 | "mode": mode, |
| 145 | "blocking": blocking, |
| 146 | "loop": loop, |
| 147 | "run_in_executor": run_in_executor, |
| 148 | "executor": executor, |
| 149 | } |
| 150 | self._context: AsyncFileLockContext = (AsyncThreadLocalFileContext if thread_local else AsyncFileLockContext)( |
| 151 | **kwargs |
| 152 | ) |
| 153 | |
| 154 | @property |
| 155 | def run_in_executor(self) -> bool: |
| 156 | """::return: whether run in executor.""" |
| 157 | return self._context.run_in_executor |
| 158 |
nothing calls this directly
no outgoing calls
no test coverage detected