https://github.com/goodmanwen/aioredlock-py
Secure and efficient distributed locks (Radisson like) implemetation
https://github.com/goodmanwen/aioredlock-py
asyncio redis redisson redlock
Last synced: about 2 months ago
JSON representation
Secure and efficient distributed locks (Radisson like) implemetation
- Host: GitHub
- URL: https://github.com/goodmanwen/aioredlock-py
- Owner: GoodManWEN
- License: mit
- Created: 2022-04-15T15:44:00.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-04-16T19:13:17.000Z (about 3 years ago)
- Last Synced: 2025-02-09T20:41:27.885Z (3 months ago)
- Topics: asyncio, redis, redisson, redlock
- Language: Python
- Homepage:
- Size: 89.8 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# aioredlock-py
[](https://pypi.org/project/aioredlock-py/)
[](https://github.com/GoodManWEN/aioredlock-py/blob/master/LICENSE)
[](https://pypi.org/project/aioredlock-py/)
[](https://github.com/GoodManWEN/aioredlock-py/actions?query=workflow:Publish)
[](https://github.com/GoodManWEN/aioredlock-py/actions?query=workflow:Build)
[](https://readthedocs.org/projects/aioredlock-py/)Secure and efficient distributed locks (Redisson like) implemetation. Ensure efficient performance with biased locking's implementation, can load more than 1k/s of concurrent requests with default parameter settings.
## Requirements
- aioredis>=2.0.0## Install
pip install aioredlock-py
## Feature
- Ensure reliability with context manager.
- Use lua scripts to ensure atomicity on lock release.
- Notification prompt you to cancel the following execution if acquisition fails
- Reliable in high concurrency.## Documentation
https://aioredlock-py.readthedocs.io## Basic usage
```python
import asyncio
import aioredis
from aioredlock_py import Redissonasync def single_thread(redis):
for _ in range(10):
async with Redisson(redis, key="no1") as lock:
if not lock:
# If the lock still fails after several attempts, `__aenter__`
# will return None to prompt you to cancel the following execution
return 'Do something, failed to acquire lock' # raise ...
# else
# Service logic protected by Redisson
await redis.incr("foo")async def test_long_term_occupancy(redis):
async with Redisson(redis, key="no1", ex=10) as lock:
if not lock: return;
# Service logic protected by Redisson
await redis.set("foo", 0)
# By default, a lock is automatically released if no action is
# taken for 20 seconds after redisson holds it. Let's assume that
# your service logic takes a long time (30s in this case) to process,
# you don't need to worry about it causing chaos, because there's
# background threads help you automatically renew legally held locks.
await asyncio.sleep(30)
await redis.incr("foo")async def main():
redis = aioredis.from_url("redis://localhost")
await redis.delete("redisson:no1")
await redis.set("foo", 0)
await asyncio.gather(*(single_thread(redis) for _ in range(20)))
assert int(await redis.get("foo")) == 200
# await test_long_term_occupancy(redis)asyncio.run(main())
```