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

Uploaded Python 3

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

Hashes for wool_locking-0.1rc7.tar.gz
Algorithm Hash digest
SHA256 48a100a47349977cc3151031a8d7dfd05a4c29b2c923c581c59424c20134d8a9
MD5 86e7f12848eb8c24a03af8054c994577
BLAKE2b-256 ecd22b0ad89c9e4913cdb3fe47d560e96b6cd54d193f46978db138854ce62ea9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for wool_locking-0.1rc7-py3-none-any.whl
Algorithm Hash digest
SHA256 3454dbe2a357e0f064bc24ab8b04fe9eb102509baa6f5e799098a245d01b2855
MD5 9185d380e73a7124ed12bb022ea653dc
BLAKE2b-256 dcd50bd4f68b8809af39f04876cbdd3f5d91d4ca5ebe6bf49fcf1e7c85249bf3

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