Skip to main content

Convenience helpers for named Redis locks.

Project description

redis-locking

Small convenience helpers for named Redis locks.

redis-locking does not implement a new distributed locking algorithm. It wraps python-redis-lock with a small application-level API for consistent lock naming, optional lock disabling, decorators, and busy-check hooks.

Install

Install from source:

git clone https://github.com/larsderidder/redis-locking.git
cd redis-locking
python -m venv .venv
. .venv/bin/activate
python -m pip install -U pip
python -m pip install .

Usage

from redis_locking import LockSettings, acquire_lock, build_key, is_locked, lock_key, locked

settings = LockSettings(key_prefix="example:", lock_expire=120, acquire_timeout=10)

with acquire_lock(redis_conn, "task:sync", settings=settings):
    pass

if is_locked(redis_conn, "task:sync", settings=settings):
    print("Busy")

key = build_key("process:", "daily", "sync")

@locked(redis_conn, key, settings=settings)
def run_task():
    pass

Busy checks

Some systems need a second readiness check after the Redis lock is acquired. For example, a device, API, or external job runner may still report that it is busy. Use lock_key for that pattern:

def is_device_busy(lock_name: str, timeout: int | None) -> bool:
    return ask_device_if_busy(lock_name, timeout=timeout)

with lock_key(
    redis_conn,
    "device:42",
    settings=settings,
    check_fn=is_device_busy,
    check_retries=2,
    check_delay=5,
    check_timeout=30,
):
    pass

Domain wrappers

You can keep lock naming consistent by creating focused wrappers:

DEFAULT_LOCKS = LockSettings(key_prefix="svc:", lock_expire=120, auto_renewal=True)

def lock_process(redis_conn, process_name: str):
    key = build_key("process:", process_name)
    return acquire_lock(redis_conn, key, settings=DEFAULT_LOCKS, blocking=False)

def is_process_locked(redis_conn, process_name: str) -> bool:
    key = build_key("process:", process_name)
    return is_locked(redis_conn, key, settings=DEFAULT_LOCKS)

Development

python -m venv .venv
. .venv/bin/activate
python -m pip install -U pip
python -m pip install -e ".[dev]"
pytest

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

redis_locking-0.1.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

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

redis_locking-0.1.0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file redis_locking-0.1.0.tar.gz.

File metadata

  • Download URL: redis_locking-0.1.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for redis_locking-0.1.0.tar.gz
Algorithm Hash digest
SHA256 db282720c2053a3c93ed273c5d6ea9f49004758729bf28f95a649afe0e3b4c7d
MD5 275baef74f2f860f0e816b42b7e0aacb
BLAKE2b-256 812103034783eebef91e09b311db98fe6b7d3fff93bf280dd708d66b93677afc

See more details on using hashes here.

Provenance

The following attestation bundles were made for redis_locking-0.1.0.tar.gz:

Publisher: publish.yml on larsderidder/redis-locking

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file redis_locking-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: redis_locking-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for redis_locking-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d4deb9d63c82c80e18602b7ce9d0f0baedf8ef4b104cf490bab99acda0bbf875
MD5 e831128838ee491b4df28f85b43ca677
BLAKE2b-256 5882b73b368325dffe34334db521b93be7fd0748371f00f88d106fc695d8a8cd

See more details on using hashes here.

Provenance

The following attestation bundles were made for redis_locking-0.1.0-py3-none-any.whl:

Publisher: publish.yml on larsderidder/redis-locking

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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