Skip to main content

aioredis_lock

CircleCI

Implementation of distributed locking with aioredis, an asyncio based redis client.

This is a standalone lib until, and if, aio-libs/aioredis#573 is accepted.

Usage

You need an aioredis.RedisConnection or aioredis.ConnectionsPool already created.

Mutex

from aioredis_lock import RedisLock, LockTimeoutError

try:
    async with RedisLock(
        pool,
        key="foobar",
        # how long until the lock should expire (seconds). this can be extended
        # via `await lock.extend(30)`
        timeout=30,
        # you can customize how long to allow the lock acquisitions to be
        # attempted.
        wait_timeout=30,
    ) as lock:
        # If you get here, you now have a lock and are the only program that
        # should be running this code at this moment.

        # do some work...

        # we may want it longer...
        await lock.extend(30)

except LockTimeoutError:
    # The lock could not be acquired by this worker and we should give up
    pass

Simple Leader/Follower(s)

Let's suppose you need a simple leader/follower type implementation where you have a number of web-workers but just want 1 to preform a repeated task. In the case the leader fails someone else should pick up the work. Simply pass wait_timeout=None to RedisLock allowing the worker to keep trying to get a lock for when the leader eventually fails. The main complication here is extending the lock and validating the leader still owns it.

from aioredis_lock import RedisLock

# if the lock is lost, we still want to be a follower
while True:

    # wait indefinitely to acquire a lock
    async with RedisLock(pool, "shared_key", wait_timeout=None) as lock:

        # hold the lock as long as possible
        while True:
            if not await lock.is_owner():
                logger.debug("We are no longer the lock owner, falling back")
                break

            # do some work

            if not await lock.renew():
                logger.debug("We lost the lock, falling back to follower mode")
                break

This mostly delegates the work of selecting and more importantly promoting leaders.

Metadata

Release files for aioredis-lock 0.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aioredis-lock 0.0.3
File Size Uploaded Provenance Actions
aioredis-lock-0.0.3.tar.gz 4.6 kB Without provenance Details for aioredis-lock-0.0.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for aioredis-lock 0.0.3
File Interpreter ABI Platform Actions
aioredis_lock-0.0.3-py3-none-any.whl Python 3 none any Details for aioredis_lock-0.0.3-py3-none-any.whl

Total release size: 10.8 kB

Release files / aioredis-lock-0.0.3.tar.gz

Download URL aioredis-lock-0.0.3.tar.gz
Size 4.6 kB
Tags Source
SHA-256 checksum
How to use checksums
ff927b0970abfcfbc77612567e8d9e82a4403f8a95b1658d9e1fec0d0dc35cf6
BLAKE2b-256 checksum
How to use checksums
36b6d5c1a56fd9b8814e784e10036a976fa7508dc67deb83af4208ecedc2e358
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3

Release files / aioredis_lock-0.0.3-py3-none-any.whl

Download URL aioredis_lock-0.0.3-py3-none-any.whl
Size 6.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e99491c9589ba04c6144505eb9a3fa8a8cd0dbdd31257b5043eb06e6110af2af
BLAKE2b-256 checksum
How to use checksums
96a49cae5410ecf0ca3cf43798cfdce249d9fd41c2a67432b59c52bd35e46a64
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3

Security for aioredis-lock 0.0.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page