Acquires the file lock or fails with a :exc:`Timeout` error. .. code-block:: python # You can use this method in the context manager (recommended) with lock.acquire(): pass # Or use an equivalent try-finally construct:
(self, timeout=None, poll_intervall=0.05)
| 224 | return self._lock_file_fd is not None |
| 225 | |
| 226 | def acquire(self, timeout=None, poll_intervall=0.05): |
| 227 | """ |
| 228 | Acquires the file lock or fails with a :exc:`Timeout` error. |
| 229 | |
| 230 | .. code-block:: python |
| 231 | |
| 232 | # You can use this method in the context manager (recommended) |
| 233 | with lock.acquire(): |
| 234 | pass |
| 235 | |
| 236 | # Or use an equivalent try-finally construct: |
| 237 | lock.acquire() |
| 238 | try: |
| 239 | pass |
| 240 | finally: |
| 241 | lock.release() |
| 242 | |
| 243 | :arg float timeout: |
| 244 | The maximum time waited for the file lock. |
| 245 | If ``timeout < 0``, there is no timeout and this method will |
| 246 | block until the lock could be acquired. |
| 247 | If ``timeout`` is None, the default :attr:`~timeout` is used. |
| 248 | |
| 249 | :arg float poll_intervall: |
| 250 | We check once in *poll_intervall* seconds if we can acquire the |
| 251 | file lock. |
| 252 | |
| 253 | :raises Timeout: |
| 254 | if the lock could not be acquired in *timeout* seconds. |
| 255 | |
| 256 | .. versionchanged:: 2.0.0 |
| 257 | |
| 258 | This method returns now a *proxy* object instead of *self*, |
| 259 | so that it can be used in a with statement without side effects. |
| 260 | """ |
| 261 | # Use the default timeout, if no timeout is provided. |
| 262 | if timeout is None: |
| 263 | timeout = self.timeout |
| 264 | |
| 265 | # Increment the number right at the beginning. |
| 266 | # We can still undo it, if something fails. |
| 267 | with self._thread_lock: |
| 268 | self._lock_counter += 1 |
| 269 | |
| 270 | lock_id = id(self) |
| 271 | lock_filename = self._lock_file |
| 272 | start_time = time.time() |
| 273 | try: |
| 274 | while True: |
| 275 | with self._thread_lock: |
| 276 | if not self.is_locked: |
| 277 | logger().debug('Attempting to acquire lock %s on %s', lock_id, lock_filename) |
| 278 | self._acquire() |
| 279 | |
| 280 | if self.is_locked: |
| 281 | logger().debug('Lock %s acquired on %s', lock_id, lock_filename) |
| 282 | break |
| 283 | elif timeout >= 0 and time.time() - start_time > timeout: |