Distributed locks in Python, similar to https://github.com/vaidik/sherlock - also with asyncio support
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.
Supports MongoDB (using unique indexes + ttl indexes for consistency and safety) for now, can be extended for other storage systems pretty easily.
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.
Add shylock to your project via pip / pipenv / poetry
# asyncio pip install shylock[motor] # or pip install shylock[pymongo]
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.
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).
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size shylock-1.1.0-py3-none-any.whl (10.5 kB)||File type Wheel||Python version py3||Upload date||Hashes View|
|Filename, size shylock-1.1.0.tar.gz (9.3 kB)||File type Source||Python version None||Upload date||Hashes View|