MCPcopy Create free account
hub / github.com/tox-dev/filelock / AsyncReadWriteLock

Class AsyncReadWriteLock

src/filelock/_sqlite/_async.py:31–176  ·  view source on GitHub ↗

Task-owned SQLite reader/writer lock. Queued writers precede new readers. A task may reenter its current mode. Changing mode while holding the lock raises :class:`RuntimeError`. :param lock_file: SQLite lock database :param timeout: seconds to wait. A value of ``-1`` uses SQLite's

Source from the content-addressed store, hash-verified

29
30
31class AsyncReadWriteLock(AsyncReadWriteBase[ReadWriteLock]):
32 """
33 Task-owned SQLite reader/writer lock. Queued writers precede new readers.
34 A task may reenter its current mode. Changing mode while holding the lock raises :class:`RuntimeError`.
35
36 :param lock_file: SQLite lock database
37 :param timeout: seconds to wait. A value of ``-1`` uses SQLite's busy-timeout limit of about 23 days.
38 :param blocking: raise :class:`~filelock.Timeout` immediately on contention when false
39 :param is_singleton: share the backend and its task holds across wrappers for this path
40 :param loop: event loop for executor calls, defaulting to the running loop
41 :param executor: caller-owned executor. The default is a dedicated worker joined by :meth:`close`.
42
43 .. versionadded:: 3.21.0
44 """
45
46 def __init__( # ruff:ignore[too-many-arguments] # public constructor: one parameter per documented lock option
47 self,
48 lock_file: str | os.PathLike[str],
49 timeout: float = -1,
50 *,
51 blocking: bool = True,
52 is_singleton: bool = True,
53 loop: asyncio.AbstractEventLoop | None = None,
54 executor: futures.Executor | None = None,
55 ) -> None:
56 super().__init__(is_singleton=is_singleton)
57 self._fork_invalidated = False
58 with fork_transition():
59 self._bind(ReadWriteLock(lock_file, timeout, blocking=blocking, is_singleton=is_singleton), loop, executor)
60 self._executor = executor or ThreadPoolExecutor(max_workers=1)
61 # The child reset needs a fully initialized executor.
62 register_fork_object(self)
63 if os.getpid() != self._creator_pid: # pragma: forked child
64 msg = "AsyncReadWriteLock construction cannot continue after fork"
65 raise RuntimeError(msg)
66
67 @asynccontextmanager
68 async def read_lock(self, timeout: float | None = None, *, blocking: bool | None = None) -> AsyncGenerator[None]:
69 """
70 Hold the lock until the context exits. See :meth:`acquire_read` for acquisition errors.
71
72 :param timeout: seconds to wait, or ``None`` for the instance default
73 :param blocking: wait on contention, or ``None`` for the instance default
74 :yields: while the lock is held
75 """
76 await self.acquire_read(timeout, blocking=blocking)
77 body_error: BaseException | None = None
78 try:
79 yield
80 except BaseException as error:
81 body_error = error
82 raise
83 finally:
84 await self._release_in_context(body_error)
85
86 async def acquire_read(
87 self, timeout: float | None = None, *, blocking: bool | None = None
88 ) -> AsyncAcquireReadWriteReturnProxy:

Calls

no outgoing calls