A Wool plugin for distributed locking.
Project description
Wool Locking
Wool Locking is an extension of the Wool framework, providing distributed locking primitives for inter-worker and inter-pool synchronization. It allows for the execution of tasks within a locking session, ensuring safe and efficient coordination in distributed environments.
Installation
Using pip
To install the package using pip, run the following command:
[uv] pip install --pre wool-locking
Cloning from GitHub
To install the package by cloning from GitHub, run the following commands:
git clone https://github.com/wool-labs/wool.git
cd wool/wool-locking
[uv] pip install .
Usage
Locking Decorator
The @wool.locking.lock decorator allows you to execute a function within a lock session. If no lock session is found in the current context, a local lock is used as a fallback.
import wool.locking
@wool.locking.lock
async def critical_section():
# Code that requires distributed locking
...
Lock Pool
The wool.locking.pool function creates a specialized worker pool for managing distributed locking tasks. It uses a single worker and integrates with LockPoolSession and LockScheduler.
import wool.locking
@wool.locking.pool(port=48900, authkey=b"lockkey")
async def locked_task():
# Code executed within the lock pool
...
Lock Pool Session
The wool.locking.session function declares a lock pool session context. This can be used to tightly couple tasks with a specific lock pool.
import wool.locking
@wool.locking.session(port=48900, authkey=b"lockkey")
@wool.task
async def locked_task():
# Code executed within the lock pool session
...
Sample Application
Below is an example of how to use Wool Locking to execute tasks with distributed locking:
Module defining locked tasks:
tasks.py
import asyncio, wool.locking
@wool.locking.lock
async def locked_task(x, y):
await asyncio.sleep(1)
return x + y
Module executing locked workflow:
main.py
import asyncio, wool.locking
from tasks import locked_task
async def main():
with wool.locking.session(port=48900, authkey=b"lockkey"):
result = await locked_task(1, 2)
print(f"Result: {result}")
asyncio.new_event_loop().run_until_complete(main())
To run the demo, first start a lock pool specifying the module defining the tasks to be executed:
wool lock-pool up --port 48900 --authkey deadbeef
Next, in a separate terminal, execute the application defined in main.py and, finally, stop the lock pool:
python main.py
wool lock-pool down --port 48900 --authkey deadbeef
License
This project is licensed under the Apache License Version 2.0.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file wool_locking-0.1rc7.tar.gz.
File metadata
- Download URL: wool_locking-0.1rc7.tar.gz
- Upload date:
- Size: 8.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
48a100a47349977cc3151031a8d7dfd05a4c29b2c923c581c59424c20134d8a9
|
|
| MD5 |
86e7f12848eb8c24a03af8054c994577
|
|
| BLAKE2b-256 |
ecd22b0ad89c9e4913cdb3fe47d560e96b6cd54d193f46978db138854ce62ea9
|
File details
Details for the file wool_locking-0.1rc7-py3-none-any.whl.
File metadata
- Download URL: wool_locking-0.1rc7-py3-none-any.whl
- Upload date:
- Size: 10.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3454dbe2a357e0f064bc24ab8b04fe9eb102509baa6f5e799098a245d01b2855
|
|
| MD5 |
9185d380e73a7124ed12bb022ea653dc
|
|
| BLAKE2b-256 |
dcd50bd4f68b8809af39f04876cbdd3f5d91d4ca5ebe6bf49fcf1e7c85249bf3
|