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

Class ReadWriteLock

src/filelock/_sqlite/_lock.py:68–483  ·  view source on GitHub ↗

SQLite reader/writer lock. Readers share a transaction. A writer holds it exclusively. A thread can nest holds of the same mode. Changing mode raises :class:`RuntimeError`. Each thread can release only its own holds. Threads that must wait for a conflicting hold need separate instances

Source from the content-addressed store, hash-verified

66
67
68class ReadWriteLock(
69 DatabaseTransaction, ReadWriteBackend, ForkResettable, ForkClassResettable, metaclass=ReadWriteLockMeta
70):
71 """
72 SQLite reader/writer lock. Readers share a transaction. A writer holds it exclusively.
73
74 A thread can nest holds of the same mode. Changing mode raises :class:`RuntimeError`. Each thread can release
75 only its own holds. Threads that must wait for a conflicting hold need separate instances with
76 ``is_singleton=False``.
77
78 Give the lock a dedicated file on a filesystem supported by SQLite. WAL databases and oversized schemas raise
79 :class:`ValueError`. In a directory writable by other accounts, this account also needs write access and
80 hard-link support. Protect the directory and its ancestors against replacement.
81
82 :param lock_file: path to the lock database
83 :param timeout: default wait in seconds. ``-1`` waits up to SQLite's busy-timeout cap of about 23 days
84 :param blocking: whether to wait when the lock is unavailable
85 :param is_singleton: reuse the instance for this resolved path
86
87 .. versionadded:: 3.21.0
88 """
89
90 _instances: ClassVar[WeakValueDictionary[pathlib.Path, ReadWriteLock]] = WeakValueDictionary()
91 _instances_lock = threading.RLock()
92 _instances_pid = GETPID()
93 _instances_under_construction: ClassVar[set[pathlib.Path]] = set()
94
95 @override
96 def __init_subclass__(cls) -> None:
97 super().__init_subclass__()
98 cls._instances = WeakValueDictionary()
99 cls._instances_lock = threading.RLock()
100 cls._instances_pid = GETPID()
101 cls._instances_under_construction = set()
102 register_fork_class(cls)
103
104 def __init__(
105 self,
106 lock_file: str | os.PathLike[str],
107 timeout: float = -1,
108 *,
109 blocking: bool = True,
110 is_singleton: bool = True, # ruff:ignore[unused-method-argument] # consumed by ReadWriteLockMeta.__call__
111 ) -> None:
112 super().__init__(lock_file)
113 self.timeout = timeout
114 self.blocking = blocking
115 self._transaction_lock = threading.Lock()
116 self._internal_lock = threading.Lock()
117 self._hold = _Hold()
118 self._acquisition_thread_ids: set[int] = set()
119 self._closed = False
120 register_fork_object(self)
121 with fork_transition(), sqlite_transition():
122 self._open_waiting(5.0, cancel_check=None).close()
123
124 @classmethod
125 def get_lock(

Calls

no outgoing calls