Try to acquire the file lock. :param timeout: maximum wait time for acquiring the lock, ``None`` means use the default :attr:`~timeout` is and if ``timeout < 0``, there is no timeout and this method will block until the lock could be acquired :param poll_interva
(
self,
timeout: float | None = None,
poll_interval: float | None = None,
*,
poll_intervall: float | None = None,
blocking: bool | None = None,
cancel_check: Callable[[], bool] | None = None,
)
| 981 | self.release(force=True) |
| 982 | |
| 983 | def acquire( |
| 984 | self, |
| 985 | timeout: float | None = None, |
| 986 | poll_interval: float | None = None, |
| 987 | *, |
| 988 | poll_intervall: float | None = None, |
| 989 | blocking: bool | None = None, |
| 990 | cancel_check: Callable[[], bool] | None = None, |
| 991 | ) -> AcquireReturnProxy: |
| 992 | """ |
| 993 | Try to acquire the file lock. |
| 994 | |
| 995 | :param timeout: maximum wait time for acquiring the lock, ``None`` means use the default :attr:`~timeout` is and |
| 996 | if ``timeout < 0``, there is no timeout and this method will block until the lock could be acquired |
| 997 | :param poll_interval: interval of trying to acquire the lock file, ``None`` means use the default |
| 998 | :attr:`~poll_interval` |
| 999 | :param poll_intervall: deprecated, kept for backwards compatibility, use ``poll_interval`` instead |
| 1000 | :param blocking: defaults to True. If False, function will return immediately if it cannot obtain a lock on the |
| 1001 | first attempt. Otherwise, this method will block until the timeout expires or the lock is acquired. |
| 1002 | :param cancel_check: a callable returning ``True`` when the acquisition should be canceled. Checked on each poll |
| 1003 | iteration. When triggered, raises :class:`~Timeout` just like an expired timeout. |
| 1004 | |
| 1005 | :returns: a context object that will unlock the file when the context is exited |
| 1006 | |
| 1007 | :raises Timeout: if fails to acquire lock within the timeout period |
| 1008 | |
| 1009 | .. code-block:: python |
| 1010 | |
| 1011 | # You can use this method in the context manager (recommended) |
| 1012 | with lock.acquire(): |
| 1013 | pass |
| 1014 | |
| 1015 | # Or use an equivalent try-finally construct: |
| 1016 | lock.acquire() |
| 1017 | try: |
| 1018 | pass |
| 1019 | finally: |
| 1020 | lock.release() |
| 1021 | |
| 1022 | .. versionchanged:: 2.0.0 |
| 1023 | |
| 1024 | This method returns now a *proxy* object instead of *self*, so that it can be used in a with statement |
| 1025 | without side effects. |
| 1026 | |
| 1027 | """ |
| 1028 | self._raise_if_inherited() |
| 1029 | if timeout is None: |
| 1030 | timeout = self._context.timeout |
| 1031 | |
| 1032 | if blocking is None: |
| 1033 | blocking = self._context.blocking |
| 1034 | |
| 1035 | if poll_intervall is not None: |
| 1036 | msg = "use poll_interval instead of poll_intervall" |
| 1037 | warnings.warn(msg, DeprecationWarning, stacklevel=2) |
| 1038 | poll_interval = poll_intervall |
| 1039 | |
| 1040 | poll_interval = poll_interval if poll_interval is not None else self._context.poll_interval |