跳转到主要内容

分布式系统互斥锁实现。

项目描述

dimutex

在多个提供者之上实现基于asyncio的分布式互斥锁的Python库。

互斥锁是一种同步原语,用于确保只有一个工作进程可以执行给定的任务。它可以用于安全地访问共享资源或将任务分配给多个工作进程。

目前,唯一实现的提供者是GCS(Google Cloud Storage)。该实现基于文章基于Google Cloud Storage的健壮分布式锁定算法(另见Ruby实现)中描述的算法。

功能

  • 异步。
  • 类型安全。
  • 原子。
  • 到期机制确保单个工作进程不会永久持有锁。
  • 支持模拟器。

安装

python3 -m pip install dimutex

使用

import dimutex

async def do_something():
    lock = dimutex.GCS(bucket='bucket-name', name='lock-name')
    async with lock:
        try:
            await lock.acquire()
        except dimutex.AlreadyAcquiredError:
            return 'already acquired'
        try:
            ... # do something with the shared resuource
        finally:
            await lock.release()

由以下支持