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,
)
| 251 | return self._context.loop |
| 252 | |
| 253 | async def acquire( # ty: ignore[invalid-method-override] |
| 254 | self, |
| 255 | timeout: float | None = None, |
| 256 | poll_interval: float | None = None, |
| 257 | *, |
| 258 | blocking: bool | None = None, |
| 259 | cancel_check: Callable[[], bool] | None = None, |
| 260 | ) -> AsyncAcquireReturnProxy: |
| 261 | """ |
| 262 | Try to acquire the file lock. |
| 263 | |
| 264 | :param timeout: maximum wait time for acquiring the lock, ``None`` means use the default |
| 265 | :attr:`~BaseFileLock.timeout` is and if ``timeout < 0``, there is no timeout and this method will block |
| 266 | until the lock could be acquired |
| 267 | :param poll_interval: interval of trying to acquire the lock file, ``None`` means use the default |
| 268 | :attr:`~BaseFileLock.poll_interval` |
| 269 | :param blocking: defaults to True. If False, function will return immediately if it cannot obtain a lock on the |
| 270 | first attempt. Otherwise, this method will block until the timeout expires or the lock is acquired. |
| 271 | :param cancel_check: a callable returning ``True`` when the acquisition should be canceled. Checked on each poll |
| 272 | iteration. When triggered, raises :class:`~Timeout` just like an expired timeout. |
| 273 | |
| 274 | :returns: a context object that will unlock the file when the context is exited |
| 275 | |
| 276 | :raises Timeout: if fails to acquire lock within the timeout period |
| 277 | |
| 278 | .. code-block:: python |
| 279 | |
| 280 | # You can use this method in the context manager (recommended) |
| 281 | with lock.acquire(): |
| 282 | pass |
| 283 | |
| 284 | # Or use an equivalent try-finally construct: |
| 285 | lock.acquire() |
| 286 | try: |
| 287 | pass |
| 288 | finally: |
| 289 | lock.release() |
| 290 | |
| 291 | """ |
| 292 | self._raise_if_inherited() |
| 293 | if timeout is None: |
| 294 | timeout = self._context.timeout |
| 295 | |
| 296 | if blocking is None: |
| 297 | blocking = self._context.blocking |
| 298 | |
| 299 | if poll_interval is None: |
| 300 | poll_interval = self._context.poll_interval |
| 301 | |
| 302 | start_time = time.perf_counter() |
| 303 | try: |
| 304 | return await self._acquire_with_admission( |
| 305 | blocking=blocking, |
| 306 | cancel_check=cancel_check, |
| 307 | timeout=timeout, |
| 308 | poll_interval=poll_interval, |
| 309 | start_time=start_time, |
| 310 | ) |
no test coverage detected