Lock strategies
LockStrategy
Base class for implementing different locking strategies.
This class provides a common interface for various locking mechanisms, ensuring
that derived classes can define their own locking behavior. The LockStrategy
class implements the context manager protocol, allowing it to be used in with
statements to acquire and release locks automatically.
Derived classes should implement the lock and unlock methods to provide
specific locking behavior, such as thread-based locks, file-based locks, or
distributed locks.
Example usage: >>> lock_strategy = SomeLockStrategy() >>> with lock_strategy: >>> # Critical section of code >>> pass
Methods: lock(): Acquire the lock. unlock(): Release the lock. enter(): Enter the context manager, calling lock(). exit(exc_type, exc_value, traceback): Exit the context manager, calling unlock().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lock_strategy
|
LockStrategy
|
A LockStrategy object to incorporate locking mechanisms. |
required |
Source code in kosh/lock_strategies.py
NoLocking
OnlyRetry
Bases: RFileLock
Only retry, no locking.
Source code in kosh/lock_strategies.py
RFileLock
Bases: LockStrategy
A Re-entrant Filelock.
Source code in kosh/lock_strategies.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | |
lock_function(func)
Decorator to wrap functions with a LockStrategy.
The lock is acquired at the start of the method and released after the method execution or after the entire generator is consumed.
This is only intended to be used on class methods
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable
|
The method to be wrapped with the lock. |
required |
Returns:
| Type | Description |
|---|---|
Callable
|
The wrapped method with applied locking strategy. |
Source code in kosh/lock_strategies.py
lock_method(func)
Decorator to wrap class methods with a LockStrategy.
This decorator ensures that the method is executed with a locking mechanism provided by the LockStrategy. For methods that return generators, the lock will remain active throughout the iteration over the generator to ensure thread safety.
The lock is acquired at the start of the method and released after the method execution or after the entire generator is consumed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable
|
The method to be wrapped with the lock. |
required |
Returns:
| Type | Description |
|---|---|
Callable
|
The wrapped method with applied locking strategy. |