在aioredis中提供分布式锁的包装器
aioredis-lock的Python项目详细描述
锁
使用基于异步的redis客户端aioredis实现分布式锁定。
这是一个独立的lib,直到并且如果,aio-libs/aioredis#573被接受。
用法
您需要已经创建了aioredis.RedisConnection
或aioredis.ConnectionsPool
。
互斥
fromaioredis_lockimportRedisLock,LockTimeoutErrortry:asyncwithRedisLock(pool,key="foobar",# how long until the lock should expire (seconds). this can be extended# via `await lock.extend(30)`timeout=30,# you can customize how long to allow the lock acquisitions to be# attempted.wait_timeout=30,)aslock:# If you get here, you now have a lock and are the only program that# should be running this code at this moment.# do some work...# we may want it longer...awaitlock.extend(30)exceptLockTimeoutError:# The lock could not be acquired by this worker and we should give uppass
简单的领导者/追随者
假设您需要一个简单的leader/follower类型的实现,其中有许多web工作者,但只希望1执行一个重复的任务。万一领导失败了,应该由别人来接手。只需通过wait_timeout=None
来重新锁定,允许工作进程在领导最终失败时继续尝试获取锁。这里的主要复杂性是扩展锁并验证领导者仍然拥有它。
fromaioredis_lockimportRedisLock# if the lock is lost, we still want to be a followerwhileTrue:# wait indefinitely to acquire a lockasyncwithRedisLock(pool,"shared_key",wait_timeout=None)aslock:# hold the lock as long as possiblewhileTrue:ifnotawaitlock.is_owner():logger.debug("We are no longer the lock owner, falling back")break# do some workifnotawaitlock.renew():logger.debug("We lost the lock, falling back to follower mode")break
这主要代表了选拔领导的工作,更重要的是提拔领导。