redguard
Distributed synchronization primitives buit on top of Redis
Installation
pip install redguard
Available primitives
Lock- distributed mutexSemaphore- distributed semaphoreRateLimiter- distributed rate limiter
Helpers
RedGuard- factory for creating primitivesSharedResourcePool- 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()
Metadata
Release files for redguard 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| redguard-0.1.1.tar.gz | 10.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| redguard-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 22.4 kB
Release files / redguard-0.1.1.tar.gz
| Download URL | redguard-0.1.1.tar.gz |
|---|---|
| Size | 10.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
62cbbf7cf691374ff9baf7dd03816c0dc8cdb2aaab065077659a9301ab7f80e1
|
|
BLAKE2b-256 checksum How to use checksums |
3192c7814fe6a92587ba5818acc4cbcc179dbd6722050d3990a28209cea9d4d8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.8
|
Release files / redguard-0.1.1-py3-none-any.whl
| Download URL | redguard-0.1.1-py3-none-any.whl |
|---|---|
| Size | 11.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
660b215ee4efc925feb75463c56af5642dcbf5b220d06a7647b60f7ca93a0ecf
|
|
BLAKE2b-256 checksum How to use checksums |
bbca5a0d722eb5ab066e126ce27d70190b64d06b1c1cc8cfec5fceb7c34016cb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.8
|