分布式锁:RedLock
jopen
10年前
RedLock 是一个使用 Python 和 Redis 实现的分布式锁。实现了来自 @antirez 的 RedLock 算法。
示例代码:
from redlock import RedLock # By default, if no redis connection details are # provided, RedLock uses redis://127.0.0.1:6379/0 lock = RedLock("distributed_lock") lock.acquire() do_something() lock.release() from redlock import RedLock with RedLock("distributed_lock"): do_something() from redlock import RedLock with RedLock("distributed_lock", connection_details=[ {host='xxx.xxx.xxx.xxx', port=6379, db=0}, {host='xxx.xxx.xxx.xxx', port=6379, db=0}, {host='xxx.xxx.xxx.xxx', port=6379, db=0}, {host='xxx.xxx.xxx.xxx', port=6379, db=0}, ] ): do_something()