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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
db282720c2053a3c93ed273c5d6ea9f49004758729bf28f95a649afe0e3b4c7d
|
|
| MD5 |
275baef74f2f860f0e816b42b7e0aacb
|
|
| BLAKE2b-256 |
812103034783eebef91e09b311db98fe6b7d3fff93bf280dd708d66b93677afc
|
Provenance
The following attestation bundles were made for redis_locking-0.1.0.tar.gz:
Publisher:
publish.yml on larsderidder/redis-locking
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
redis_locking-0.1.0.tar.gz -
Subject digest:
db282720c2053a3c93ed273c5d6ea9f49004758729bf28f95a649afe0e3b4c7d - Sigstore transparency entry: 1573387898
- Sigstore integration time:
-
Permalink:
larsderidder/redis-locking@fb6bb09a6ff49aa8aa18609c58e97222c3a958fb -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/larsderidder
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@fb6bb09a6ff49aa8aa18609c58e97222c3a958fb -
Trigger Event:
push
-
Statement type:
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d4deb9d63c82c80e18602b7ce9d0f0baedf8ef4b104cf490bab99acda0bbf875
|
|
| MD5 |
e831128838ee491b4df28f85b43ca677
|
|
| BLAKE2b-256 |
5882b73b368325dffe34334db521b93be7fd0748371f00f88d106fc695d8a8cd
|
Provenance
The following attestation bundles were made for redis_locking-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on larsderidder/redis-locking
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
redis_locking-0.1.0-py3-none-any.whl -
Subject digest:
d4deb9d63c82c80e18602b7ce9d0f0baedf8ef4b104cf490bab99acda0bbf875 - Sigstore transparency entry: 1573387930
- Sigstore integration time:
-
Permalink:
larsderidder/redis-locking@fb6bb09a6ff49aa8aa18609c58e97222c3a958fb -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/larsderidder
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@fb6bb09a6ff49aa8aa18609c58e97222c3a958fb -
Trigger Event:
push
-
Statement type: