Distributed synchronization primitives buit on top of Redis
Project description
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()
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 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
62cbbf7cf691374ff9baf7dd03816c0dc8cdb2aaab065077659a9301ab7f80e1
|
|
| MD5 |
92cfe90af7c2f2dca063c124754c577e
|
|
| BLAKE2b-256 |
3192c7814fe6a92587ba5818acc4cbcc179dbd6722050d3990a28209cea9d4d8
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
660b215ee4efc925feb75463c56af5642dcbf5b220d06a7647b60f7ca93a0ecf
|
|
| MD5 |
9f601e9236604a9d7209e598ea3b319b
|
|
| BLAKE2b-256 |
bbca5a0d722eb5ab066e126ce27d70190b64d06b1c1cc8cfec5fceb7c34016cb
|