Skip to main content

Redis-based read-write distributed and hierarchical locking library

Project description

Redis Hierarchical Distributed Read-Write Locking

This is a lightweight implementation of a hierarchical distributed read-write lock using Redis.

It supports concurrent readers and exclusive writers in a tree-like hierarchy, ensuring locks on ancestors affect descendants.

Features

  • Hierarchical locking with customizable path separators (e.g., /, :).
  • Concurrent read locks on the same path or ancestors.
  • Exclusive write locks on paths or descendants.
  • Timeout and non-blocking lock options.
  • Automatic lock refreshing for long-running operations.

Installation

pip install redishilok

Usage

import asyncio
from redishilok import RedisHiLok

async def main():
    hilok = RedisHiLok('redis://localhost:6379/0')

    # Acquire a read lock
    async with hilok.read('a/b'):
        print("Read lock acquired on 'a/b'")

        # Acquire a write lock with non-blocking mode
        try:
            async with hilok.write('a', block=False):
                print("Write lock acquired on 'a'")
            print("Never gets here")
        except RuntimeError:
            print("Failed to acquire write lock on 'a'")

asyncio.run(main())

Examples

Basic Hierarchical Locking

import asyncio
from redishilok import RedisHiLok

async def main():
    hilok = RedisHiLok('redis://localhost:6379/0')

    # Concurrent readers
    async with hilok.read('a'):
        async with hilok.read('a/b/c'):
            print("Both read locks succeed")

    # Write lock blocks descendants
    async with hilok.write('a'):
        try:
            async with hilok.read('a/b/c', timeout=0.1):
                pass
        except RuntimeError:
            print("Failed to acquire read lock on 'a/b/c' due to write lock on 'a'")

asyncio.run(main())

Custom Separator

import asyncio
from redishilok import RedisHiLok

async def main():
    hilok = RedisHiLok('redis://localhost', separator=':')
    async with hilok.write('a:b:c'):
        print("Write lock acquired on 'a:b:c'")

asyncio.run(main())

Long-Running Operations with Refresh

import asyncio
from redishilok import RedisHiLok

async def main():
    hilok = RedisHiLok('redis://localhost', ttl=2000, refresh_interval=1000)
    async with hilok.write('a/b'):
        print("Long operation starts")
        await asyncio.sleep(5)  # Lock is automatically refreshed

asyncio.run(main())

Limitations

  • Requires a running Redis instance.
  • Not suitable for high-frequency locking scenarios (due to Redis round-trips).
  • Lock fairness is not guaranteed (e.g., no queue for blocked writers).

License

MIT

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

redishilok-1.0.1.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

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

redishilok-1.0.1-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file redishilok-1.0.1.tar.gz.

File metadata

  • Download URL: redishilok-1.0.1.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.3 Windows/10

File hashes

Hashes for redishilok-1.0.1.tar.gz
Algorithm Hash digest
SHA256 2408ad29edcfc965e5928922b8f7f1f10b1a5349ecb545c59da128240b06a38f
MD5 f9d009f17659c5e2913db58dd6af53ce
BLAKE2b-256 10a704305e6469b718539abcee50bb2d40ceedae037a75199a168d12219e427c

See more details on using hashes here.

File details

Details for the file redishilok-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: redishilok-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.3 Windows/10

File hashes

Hashes for redishilok-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b828e70e80fdf8597c5d46c6a6649334985a687bde1fdec001776645e231e591
MD5 e51e867adc7fe0d20fe9a0d2f357bfe2
BLAKE2b-256 cef926cb37f6a0e4f8e0d2946b9907fee7ba8aee01d8d6e67dc7fb3332c10f42

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