Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wool_locking-0.1rc9.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

wool_locking-0.1rc9-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file wool_locking-0.1rc9.tar.gz.

File metadata

  • Download URL: wool_locking-0.1rc9.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.0

File hashes

Hashes for wool_locking-0.1rc9.tar.gz
Algorithm Hash digest
SHA256 bebc6ebc999330c2a2b066537479834daca50666ea7388ceaf244066efbb2bc1
MD5 5c99aee6658a4de02860552134ad57f5
BLAKE2b-256 456bbec656e8f783bc5b819b966e1307a9007a21a3d287bd7d0d439f6c13c85a

See more details on using hashes here.

File details

Details for the file wool_locking-0.1rc9-py3-none-any.whl.

File metadata

File hashes

Hashes for wool_locking-0.1rc9-py3-none-any.whl
Algorithm Hash digest
SHA256 821a9475ce74b027db6be6a6bd93fda92fab2afd922cea50c7cc04fd2a4f13fc
MD5 6e94c887a40119503e581776af70305a
BLAKE2b-256 405333f864e93ac5b2a4dfa17e0d648a852488ef9803d9ea49e58bc3d50d006e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page