Skip to main content

Distributed synchronization primitives buit on top of Redis

Project description

Tests Build License Python Format PyPi Mypy Ruff security: bandit

redguard

Distributed synchronization primitives buit on top of Redis

Installation

pip install redguard

Available primitives

  • Lock - distributed mutex
  • Semaphore - distributed semaphore
  • RateLimiter - distributed rate limiter

Helpers

  • RedGuard - factory for creating primitives
  • SharedResourcePool - factory for creating shared resources

Usage

The api is similar to built-in asyncio module primitives. Each primitive (except for RateLimiter) has ttl parameter which defines how long the lock is held in case of unexpected failure.

from redguard import RedGuard
from redguard.lock import Lock

guard = RedGuard.from_url("redis://localhost:6379", namespace="examples")

async def lock_example():
    lock = guard.new(Lock, "my-lock", ttl=10)

    async with lock:
        print("Locked")

async def semaphore_example():
    semaphore = guard.new(Semaphore, "my-semaphore", capacity=2, ttl=10)

    async with semaphore:
        print("Acquired")

async def rate_limiter_example():
    rate_limiter = guard.new(RateLimiter, "my-rate-limiter", limit=2, window=1)

    async with rate_limiter:
        print("Rate limited")

async def object_pool_example():
    pool = guard.pool(Semaphore, "my-pool", factory=dict, capacity=3, ttl=10)
    # this will create new dictionary limited to 3 instances globally
    async with pool as resource:
        resource["key"] = "value"
        print(resource)

Lower level api

Each primitve can be used as async context manager, but also provides acquire and release methods.

semaphore = guard.new(Semaphore, "my-semaphore", capacity=2, ttl=10)

acquired = await semaphore.acquire(blocking=True, timeout=None) # returns True if acquired (useful for blocking=False)

if acquired:
    await semaphore.release()

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

redguard-0.1.1.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

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

redguard-0.1.1-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file redguard-0.1.1.tar.gz.

File metadata

  • Download URL: redguard-0.1.1.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for redguard-0.1.1.tar.gz
Algorithm Hash digest
SHA256 62cbbf7cf691374ff9baf7dd03816c0dc8cdb2aaab065077659a9301ab7f80e1
MD5 92cfe90af7c2f2dca063c124754c577e
BLAKE2b-256 3192c7814fe6a92587ba5818acc4cbcc179dbd6722050d3990a28209cea9d4d8

See more details on using hashes here.

File details

Details for the file redguard-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: redguard-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for redguard-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 660b215ee4efc925feb75463c56af5642dcbf5b220d06a7647b60f7ca93a0ecf
MD5 9f601e9236604a9d7209e598ea3b319b
BLAKE2b-256 bbca5a0d722eb5ab066e126ce27d70190b64d06b1c1cc8cfec5fceb7c34016cb

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