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_interval:
(
self,
timeout: float | None = None,
poll_interval: float = 0.05,
*,
poll_intervall: float | None = None,
blocking: bool | None = None,
)
| 269 | return self._context.lock_counter |
| 270 | |
| 271 | def acquire( |
| 272 | self, |
| 273 | timeout: float | None = None, |
| 274 | poll_interval: float = 0.05, |
| 275 | *, |
| 276 | poll_intervall: float | None = None, |
| 277 | blocking: bool | None = None, |
| 278 | ) -> AcquireReturnProxy: |
| 279 | """ |
| 280 | Try to acquire the file lock. |
| 281 | |
| 282 | :param timeout: maximum wait time for acquiring the lock, ``None`` means use the default :attr:`~timeout` is and |
| 283 | if ``timeout < 0``, there is no timeout and this method will block until the lock could be acquired |
| 284 | :param poll_interval: interval of trying to acquire the lock file |
| 285 | :param poll_intervall: deprecated, kept for backwards compatibility, use ``poll_interval`` instead |
| 286 | :param blocking: defaults to True. If False, function will return immediately if it cannot obtain a lock on the |
| 287 | first attempt. Otherwise, this method will block until the timeout expires or the lock is acquired. |
| 288 | :raises Timeout: if fails to acquire lock within the timeout period |
| 289 | :return: a context object that will unlock the file when the context is exited |
| 290 | |
| 291 | .. code-block:: python |
| 292 | |
| 293 | # You can use this method in the context manager (recommended) |
| 294 | with lock.acquire(): |
| 295 | pass |
| 296 | |
| 297 | # Or use an equivalent try-finally construct: |
| 298 | lock.acquire() |
| 299 | try: |
| 300 | pass |
| 301 | finally: |
| 302 | lock.release() |
| 303 | |
| 304 | .. versionchanged:: 2.0.0 |
| 305 | |
| 306 | This method returns now a *proxy* object instead of *self*, |
| 307 | so that it can be used in a with statement without side effects. |
| 308 | |
| 309 | """ |
| 310 | # Use the default timeout, if no timeout is provided. |
| 311 | if timeout is None: |
| 312 | timeout = self._context.timeout |
| 313 | |
| 314 | if blocking is None: |
| 315 | blocking = self._context.blocking |
| 316 | |
| 317 | if poll_intervall is not None: |
| 318 | msg = "use poll_interval instead of poll_intervall" |
| 319 | warnings.warn(msg, DeprecationWarning, stacklevel=2) |
| 320 | poll_interval = poll_intervall |
| 321 | |
| 322 | # Increment the number right at the beginning. We can still undo it, if something fails. |
| 323 | self._context.lock_counter += 1 |
| 324 | |
| 325 | lock_id = id(self) |
| 326 | lock_filename = self.lock_file |
| 327 | start_time = time.perf_counter() |
| 328 | try: |