Skip to main content

Distributed locks in Python, similar to https://github.com/vaidik/sherlock - also with asyncio support

Project description

https://app.fossa.io/api/projects/git%2Bgithub.com%2Flietu%2Fshylock.svg?type=shield https://img.shields.io/github/actions/workflow/status/lietu/shylock/publish.yaml https://img.shields.io/badge/code%20style-black-000000.svg https://codecov.io/gh/lietu/shylock/branch/master/graph/badge.svg https://sonarcloud.io/api/project_badges/measure?project=lietu_shylock&metric=alert_status GitHub issues PyPI - Downloads PyPI PyPI - Python Version https://img.shields.io/badge/License-BSD%203--Clause-blue.svg

Distributed locks on Python, with asyncio support

What is this?

Locks are required when you have a distributed system (like any API) and you want to ensure consistency for your data and prevent race conditions. There are a lot of ways to implement them, and this library aims to provide easy access to some of the better ways.

The library is written primarily for use with asyncio code, but also supports normal synchronous usage.

Currently supported backends:

  • MongoDB (using unique indexes + ttl indexes for consistency and safety)

  • ArangoDB (using unique indexes + ttl indexes for consistency and safety)

Can be extended for other storage systems pretty easily.

License

Licensing is important. This project itself uses BSD 3-clause license, but e.g. Mongodb Motor library and other such libraries used by it may have their own licenses.

For more information check the LICENSE -file.

Getting started

Add shylock to your project via pip / pipenv / poetry

# MongoDB asyncio
pip install shylock[motor]
# MongoDB
pip install shylock[pymongo]
# ArangoDB asyncio
pip install shylock[aioarangodb]
# ArangoDB
pip install shylock[python-arango]

For most easy usage, you should in your application startup logic configure the default backend for Shylock to use, and then use the AsyncLock class to handle your locking needs.

from motor.motor_asyncio import AsyncIOMotorClient

from shylock import configure, AsyncLock as Lock, ShylockMotorAsyncIOBackend

CONNECTION_STRING = "mongodb://your-connection-string"

client = AsyncIOMotorClient(CONNECTION_STRING)
configure(await ShylockMotorAsyncIOBackend.create(client, "projectdb"))

async def use_lock():
    async with Lock("my-lock"):
        # The lock is now acquired, and will be automatically released
        do_something()

async def another_lock_use():
    lock = Lock("my-lock")
    try:
        await lock.acquire()
        do_something()
    finally:
         await lock.release()

async def time_sensitive_code():
    lock = Lock("my-lock")
    try:
        locked = await lock.acquire(block=False)
        if locked:
            do_something()
    finally:
         if locked:
             await lock.release()

Or the Lock class for code where asyncio support isn’t required

from pymongo import MongoClient

from shylock import configure, Lock, ShylockPymongoBackend

CONNECTION_STRING = "mongodb://your-connection-string"

client = MongoClient(CONNECTION_STRING)
configure(ShylockPymongoBackend.create(client, "projectdb"))

def use_lock():
    with Lock("my-lock"):
        # The lock is now acquired, and will be automatically released
        do_something()

def another_lock_use():
    lock = Lock("my-lock")
    try:
        lock.acquire()
        do_something()
    finally:
         lock.release()

def time_sensitive_code():
    lock = Lock("my-lock")
    try:
        locked = lock.acquire(block=False)
        if locked:
            do_something()
    finally:
         if locked:
             lock.release()

You can also check out the examples, which also show how to use Shylock with ArangoDB.

Contributing

This project is run on GitHub using the issue tracking and pull requests here. If you want to contribute, feel free to submit issues (incl. feature requests) or PRs here.

To test changes locally python setup.py develop is a good way to run this, and you can python setup.py develop --uninstall afterwards (you might want to also use the --user flag).

https://app.fossa.io/api/projects/git%2Bgithub.com%2Flietu%2Fshylock.svg?type=large

Financial support

This project has been made possible thanks to Cocreators and Lietu. You can help us continue our open source work by supporting us on Buy me a coffee.

https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

shylock-1.2.1.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

shylock-1.2.1-py3-none-any.whl (13.8 kB view details)

Uploaded Python 3

File details

Details for the file shylock-1.2.1.tar.gz.

File metadata

  • Download URL: shylock-1.2.1.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.10.9 Linux/5.15.0-1031-azure

File hashes

Hashes for shylock-1.2.1.tar.gz
Algorithm Hash digest
SHA256 8101fc745ae85fc1f5e1e748bd7c84cdd5568ea4dd367c39449ab17dafad35e1
MD5 dbb154f6766094bb51581967d8286c02
BLAKE2b-256 236979c28d6fa6751f85080432ac184d1588d5a4ca1ff898ee776614f8d99dc9

See more details on using hashes here.

File details

Details for the file shylock-1.2.1-py3-none-any.whl.

File metadata

  • Download URL: shylock-1.2.1-py3-none-any.whl
  • Upload date:
  • Size: 13.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.2 CPython/3.10.9 Linux/5.15.0-1031-azure

File hashes

Hashes for shylock-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3e717172939f6a5fb9559ec7b77137250d43e26eab42e0ee8c2d897645d7b7d7
MD5 05b917c8b1011cdd5c35d2c8060826eb
BLAKE2b-256 13da27d32bbd54f9be57db54a9ca3c7c4f26f5840fc804b6f51275c07d73de8f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page