Skip to content

asyncio Python library for distributed mutex with GCS as a backend

License

Notifications You must be signed in to change notification settings

orsinium-labs/dimutex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

33 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dimutex

Python library implementing asyncio-based distributed mutex on top of different providers.

Mutex is a synchronization primitive used to ensure that only one worker can do the given job. It can be used for safe access to a shared resource or for distributing tasks among multiple workers.

Currently, the only implemented provider is GCS (Google Cloud Storage). The implementation is based on the algorithm described in article A robust distributed locking algorithm based on Google Cloud Storage (see also Ruby implementation).

Features

  • Asynchronous.
  • Type-safe.
  • Atomic.
  • Expiration mechanism to ensure that a single worker won't hold the lock forever.
  • Supports emulators.

Installation

python3 -m pip install dimutex

Usage

import dimutex

async def do_something():
    lock = dimutex.GCS(bucket='bucket-name', name='lock-name')
    # context manager makes sure to close aiohttp session
    async with lock:
        try:
            await lock.acquire()
        except dimutex.AlreadyAcquiredError:
            return 'already acquired'
        try:
            # do something with the shared resource
            ...
            # update expiration if you need more time
            await lock.refresh()
            ...
        finally:
            await lock.release()