Try to acquire the file lock. :param timeout: maximum wait time for acquiring the lock, ``None`` means use the default :attr:`~BaseFileLock.timeout` is and if ``timeout < 0``, there is no timeout and this method will block until the lock could be acquired
( # ty: ignore[invalid-method-override]
self,
timeout: float | None = None,
poll_interval: float | None = None,
*,
blocking: bool | None = None,
cancel_check: Callable[[], bool] | None = None,
)
| 205 | return self._context.loop |
| 206 | |
| 207 | async def acquire( # ty: ignore[invalid-method-override] |
| 208 | self, |
| 209 | timeout: float | None = None, |
| 210 | poll_interval: float | None = None, |
| 211 | *, |
| 212 | blocking: bool | None = None, |
| 213 | cancel_check: Callable[[], bool] | None = None, |
| 214 | ) -> AsyncAcquireReturnProxy: |
| 215 | """ |
| 216 | Try to acquire the file lock. |
| 217 | |
| 218 | :param timeout: maximum wait time for acquiring the lock, ``None`` means use the default |
| 219 | :attr:`~BaseFileLock.timeout` is and if ``timeout < 0``, there is no timeout and this method will block |
| 220 | until the lock could be acquired |
| 221 | :param poll_interval: interval of trying to acquire the lock file, ``None`` means use the default |
| 222 | :attr:`~BaseFileLock.poll_interval` |
| 223 | :param blocking: defaults to True. If False, function will return immediately if it cannot obtain a lock on the |
| 224 | first attempt. Otherwise, this method will block until the timeout expires or the lock is acquired. |
| 225 | :param cancel_check: a callable returning ``True`` when the acquisition should be canceled. Checked on each poll |
| 226 | iteration. When triggered, raises :class:`~Timeout` just like an expired timeout. |
| 227 | |
| 228 | :returns: a context object that will unlock the file when the context is exited |
| 229 | |
| 230 | :raises Timeout: if fails to acquire lock within the timeout period |
| 231 | |
| 232 | .. code-block:: python |
| 233 | |
| 234 | # You can use this method in the context manager (recommended) |
| 235 | with lock.acquire(): |
| 236 | pass |
| 237 | |
| 238 | # Or use an equivalent try-finally construct: |
| 239 | lock.acquire() |
| 240 | try: |
| 241 | pass |
| 242 | finally: |
| 243 | lock.release() |
| 244 | |
| 245 | """ |
| 246 | # Use the default timeout, if no timeout is provided. |
| 247 | if timeout is None: |
| 248 | timeout = self._context.timeout |
| 249 | |
| 250 | if blocking is None: |
| 251 | blocking = self._context.blocking |
| 252 | |
| 253 | if poll_interval is None: |
| 254 | poll_interval = self._context.poll_interval |
| 255 | |
| 256 | # Increment the number right at the beginning. We can still undo it, if something fails. |
| 257 | self._context.lock_counter += 1 |
| 258 | |
| 259 | start_time = time.perf_counter() |
| 260 | try: |
| 261 | await self._async_poll_until_acquired( |
| 262 | blocking=blocking, |
| 263 | cancel_check=cancel_check, |
| 264 | timeout=timeout, |
no test coverage detected