在aioredis中提供分布式锁的包装器

aioredis-lock的Python项目详细描述


CircleCI

使用基于异步的redis客户端aioredis实现分布式锁定。

这是一个独立的lib,直到并且如果,aio-libs/aioredis#573被接受。

用法

您需要已经创建了aioredis.RedisConnectionaioredis.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

这主要代表了选拔领导的工作,更重要的是提拔领导。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java连接usb到uart设备到安卓设备>3.1   可以强制Php中的web应用程序与Java中的桌面应用程序一起工作吗?   java为什么自定义系统类加载器不工作?   数组在Java中解析具有多个分隔符的字符串   PMD Java 8德米特定律   JavaSpringMVC表单验证不适用于嵌套的复杂类型   让Eclipse Java组织导入以使用Google checkstyle   java Appium:无法创建新会话   java如何在数组中声明新字段   java如何解决“无法初始化类org.apache.cassandra.config.DatabaseDescriptor”?   java AsyncTask创建socket   java向@CreatedBy添加更多信息   如何在ubuntu中运行包含大量jars依赖项的java文件   java如何使用<s:select>标记并在中休眠来填充下拉列表?   java获取错误:找不到符号变量“level”和“next_level_button”   javaweb应用中基于UI的ajax显示代码流   Java长到MySql   java JvisualVM:奇怪的应用程序行为   ubuntu将Java程序的输出结果保存到一个文件中