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.1rc8.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.1rc8-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for wool_locking-0.1rc8.tar.gz
Algorithm Hash digest
SHA256 44c16375e9233ffec9b12c766e2b1f0bbd387874f283d2524d2fb144b6739c44
MD5 51e5f585a0c0a6e5cb1a379700763c23
BLAKE2b-256 f6d0ceaf1db55f1166076557be281ecb0875e206030ae7f550a5c2cedd66d953

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for wool_locking-0.1rc8-py3-none-any.whl
Algorithm Hash digest
SHA256 0c2afab8fa1b2e8fa840f33d2b8156e01c29d087a82799dbf3a81ab626f6cf75
MD5 063ddb73bbde1c501f3c85b3f796d03d
BLAKE2b-256 9f97c6a0dc4aea6fe9367ca48c1d37b896ec02643025e30dbc7d912d7b13955e

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