Sherlock: Distributed Locks with a choice of backend
====================================================
Sherlock is a library that provides easy-to-use distributed inter-process
locks and also allows you to choose a backend of your choice for lock
synchronization.
|Build Status| |Coverage Status|
.. |Build Status| image:: https://github.com/py-sherlock/sherlock/actions/workflows/ci.yml/badge.svg
:target: https://github.com/py-sherlock/sherlock/actions?query=workflow%3ACI/CD
.. |Coverage Status| image:: https://codecov.io/gh/py-sherlock/sherlock/branch/master/graph/badge.svg?token=QJXCZVSAEF
:target: https://codecov.io/gh/py-sherlock/sherlock
Overview
--------
When you are working with resources which are accessed by multiple services or
distributed services, more than often you need some kind of locking mechanism
to make it possible to access some resources at a time.
Distributed Locks or Mutexes can help you with this. Sherlock provides
the exact same facility, with some extra goodies. It provides an easy-to-use API
that resembles standard library's `threading.Lock` semantics.
Apart from this, Sherlock gives you the flexibility of using a backend of
your choice for managing locks.
Sherlock also makes it simple for you to extend Sherlock to use
backends that are not supported.
Features
++++++++
* API similar to standard library's `threading.Lock`.
* Support for With statement, to cleanly acquire and release locks.
* Backend agnostic: supports File, `Redis`_, `Memcached`_, `Etcd`_, and `Kubernetes`_ as choice of
backends.
* Extendable: can be easily extended to work with any other of backend of
choice by extending base lock class. Read ``extending``.
.. _Redis: http://redis.io
.. _Memcached: http://memcached.org
.. _Etcd: http://github.com/coreos/etcd
.. _Kubernetes: https://kubernetes.io
Supported Backends and Client Libraries
+++++++++++++++++++++++++++++++++++++++
Following client libraries are supported for every supported backend:
* File: `filelock <https://github.com/tox-dev/py-filelock>`__
* Redis: `redis-py <https://github.com/redis/redis-py/tree/master/redis>`__
* Memcached: `pylibmc <https://github.com/lericson/pylibmc>`__
* Etcd: `python-etcd <https://github.com/jplana/python-etcd>`__
* Kubernetes: `kubernetes <https://github.com/kubernetes-client/python>`__
As of now, only the above mentioned libraries are supported. Although
Sherlock takes custom client objects so that you can easily provide
settings that you want to use for that backend store, but Sherlock also
checks if the provided client object is an instance of the supported clients
and accepts client objects which pass this check, even if the APIs are the
same. Sherlock might get rid of this issue later, if need be and if
there is a demand for that.
Installation
------------
Installation is simple.
.. code:: bash
pip install sherlock[all]
.. note:: Sherlock will install all the client libraries for all the
supported backends.
Basic Usage
-----------
Sherlock is simple to use as at the API and semantics level, it tries to
conform to standard library's ``threading.Lock`` APIs.
.. code-block:: python
import sherlock
from sherlock import Lock
# Configure Sherlock's locks to use Redis as the backend,
# never expire locks and retry acquiring an acquired lock after an
# interval of 0.1 second.
sherlock.configure(backend=sherlock.backends.REDIS,
expire=None,
retry_interval=0.1)
# Note: configuring sherlock to use a backend does not limit you
# another backend at the same time. You can import backend specific locks
# like RedisLock, MCLock and EtcdLock and use them just the same way you
# use a generic lock (see below). In fact, the generic Lock provided by
# sherlock is just a proxy that uses these specific locks under the hood.
# acquire a lock called my_lock
lock = Lock('my_lock')
# acquire a blocking lock
lock.acquire()
# check if the lock has been acquired or not
lock.locked() == True
# attempt to renew the lock
lock.renew()
# release the lock
lock.release()
Support for ``with`` statement
++++++++++++++++++++++++++++++
.. code-block:: python
# using with statement
with Lock('my_lock') as lock:
# do something constructive with your locked resource here
pass
Blocking and Non-blocking API
+++++++++++++++++++++++++++++
.. code-block:: python
# acquire non-blocking lock
lock1 = Lock('my_lock')
lock2 = Lock('my_lock')
# successfully acquire lock1
lock1.acquire()
# try to acquire lock in a non-blocking way
lock2.acquire(False) == True # returns False
# try to acquire lock in a blocking way
lock2.acquire() # blocks until lock is acquired to timeout happens
Using two backends at the same time
+++++++++++++++++++++++++++++++++++
Configuring Sherlock to use a backend does not limit you from using
another backend at the same time. You can import backend specific locks like
RedisLock, MCLock and EtcdLock and use them just the same way you use a generic
lock (see below). In fact, the generic Lock provided by Sherlock is just
a proxy that uses these specific locks under the hood.
.. code-block:: python
import sherlock
from sherlock import Lock
# Configure Sherlock's locks to use Redis as the backend
sherlock.configure(backend=sherlock.backends.REDIS)
# Acquire a lock called my_lock, this lock uses Redis
lock = Lock('my_lock')
# Now acquire locks in Memcached
from sherlock import MCLock
mclock = MCLock('my_mc_lock')
mclock.acquire()
Tests
-----
To run all the tests (including integration), you have to make sure that all
the databases are running. Make sure all the services are running:
.. code:: bash
# memcached
memcached
# redis-server
redis-server
# etcd (etcd is probably not available as package, here is the simplest way
# to run it).
wget https://github.com/coreos/etcd/releases/download/<version>/etcd-<version>-<platform>.tar.gz
tar -zxvf etcd-<version>-<platform>.gz
./etcd-<version>-<platform>/etcd
Run tests like so:
.. code:: bash
python setup.py test
Documentation
-------------
Available `here`_.
.. _here: http://sher-lock.readthedocs.org
License
-------
See `LICENSE`_.
**In short**: This is an open-source project and exists for anyone to use it
and/or modify it for personal use. Just be nice and attribute the credits
wherever you can. :)
.. _LICENSE: http://github.com/vaidik/sherlock/blob/master/LICENSE.rst
Questions?
----------
You are encouraged to ask questions using `issues`_ as that helps everyone and
myself when people with better know-how contribute to the discussion. However,
if you wish to get in touch with me personally, then I can be contacted at
**kapoor.vaidik++github+sherlock@gmail.com**.
.. _issues: https://github.com/vaidik/sherlock/issues
Distributed Locking in Other Languages
--------------------------------------
* NodeJS - https://github.com/thedeveloper/warlock
Raw data
{
"_id": null,
"home_page": "https://github.com/py-sherlock/sherlock",
"name": "sherlock",
"maintainer": "Vaidik Kapoor",
"docs_url": null,
"requires_python": ">=3.7,<4.0",
"maintainer_email": "kapoor.vaidik@gmail.com",
"keywords": "locking",
"author": "Vaidik Kapoor",
"author_email": "kapoor.vaidik@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/17/3c/4f44dbb806efdd86005b3ac183a6c9c27f361e3ef473e41f5f553758ea6f/sherlock-0.4.1.tar.gz",
"platform": null,
"description": "\nSherlock: Distributed Locks with a choice of backend\n====================================================\n\nSherlock is a library that provides easy-to-use distributed inter-process\nlocks and also allows you to choose a backend of your choice for lock\nsynchronization.\n\n|Build Status| |Coverage Status|\n\n.. |Build Status| image:: https://github.com/py-sherlock/sherlock/actions/workflows/ci.yml/badge.svg\n :target: https://github.com/py-sherlock/sherlock/actions?query=workflow%3ACI/CD\n\n.. |Coverage Status| image:: https://codecov.io/gh/py-sherlock/sherlock/branch/master/graph/badge.svg?token=QJXCZVSAEF\n :target: https://codecov.io/gh/py-sherlock/sherlock\n\nOverview\n--------\n\nWhen you are working with resources which are accessed by multiple services or\ndistributed services, more than often you need some kind of locking mechanism\nto make it possible to access some resources at a time.\n\nDistributed Locks or Mutexes can help you with this. Sherlock provides\nthe exact same facility, with some extra goodies. It provides an easy-to-use API\nthat resembles standard library's `threading.Lock` semantics.\n\nApart from this, Sherlock gives you the flexibility of using a backend of\nyour choice for managing locks.\n\nSherlock also makes it simple for you to extend Sherlock to use\nbackends that are not supported.\n\nFeatures\n++++++++\n\n* API similar to standard library's `threading.Lock`.\n* Support for With statement, to cleanly acquire and release locks.\n* Backend agnostic: supports File, `Redis`_, `Memcached`_, `Etcd`_, and `Kubernetes`_ as choice of\n backends.\n* Extendable: can be easily extended to work with any other of backend of\n choice by extending base lock class. Read ``extending``.\n\n.. _Redis: http://redis.io\n.. _Memcached: http://memcached.org\n.. _Etcd: http://github.com/coreos/etcd\n.. _Kubernetes: https://kubernetes.io\n\nSupported Backends and Client Libraries\n+++++++++++++++++++++++++++++++++++++++\n\nFollowing client libraries are supported for every supported backend:\n\n* File: `filelock <https://github.com/tox-dev/py-filelock>`__\n* Redis: `redis-py <https://github.com/redis/redis-py/tree/master/redis>`__\n* Memcached: `pylibmc <https://github.com/lericson/pylibmc>`__\n* Etcd: `python-etcd <https://github.com/jplana/python-etcd>`__\n* Kubernetes: `kubernetes <https://github.com/kubernetes-client/python>`__\n\nAs of now, only the above mentioned libraries are supported. Although\nSherlock takes custom client objects so that you can easily provide\nsettings that you want to use for that backend store, but Sherlock also\nchecks if the provided client object is an instance of the supported clients\nand accepts client objects which pass this check, even if the APIs are the\nsame. Sherlock might get rid of this issue later, if need be and if\nthere is a demand for that.\n\nInstallation\n------------\n\nInstallation is simple.\n\n.. code:: bash\n\n pip install sherlock[all]\n\n.. note:: Sherlock will install all the client libraries for all the\n supported backends.\n\nBasic Usage\n-----------\n\nSherlock is simple to use as at the API and semantics level, it tries to\nconform to standard library's ``threading.Lock`` APIs.\n\n.. code-block:: python\n\n import sherlock\n from sherlock import Lock\n\n # Configure Sherlock's locks to use Redis as the backend,\n # never expire locks and retry acquiring an acquired lock after an\n # interval of 0.1 second.\n sherlock.configure(backend=sherlock.backends.REDIS,\n expire=None,\n retry_interval=0.1)\n\n # Note: configuring sherlock to use a backend does not limit you\n # another backend at the same time. You can import backend specific locks\n # like RedisLock, MCLock and EtcdLock and use them just the same way you\n # use a generic lock (see below). In fact, the generic Lock provided by\n # sherlock is just a proxy that uses these specific locks under the hood.\n\n # acquire a lock called my_lock\n lock = Lock('my_lock')\n\n # acquire a blocking lock\n lock.acquire()\n\n # check if the lock has been acquired or not\n lock.locked() == True\n\n # attempt to renew the lock\n lock.renew()\n\n # release the lock\n lock.release()\n\nSupport for ``with`` statement\n++++++++++++++++++++++++++++++\n\n.. code-block:: python\n\n # using with statement\n with Lock('my_lock') as lock:\n # do something constructive with your locked resource here\n pass\n\nBlocking and Non-blocking API\n+++++++++++++++++++++++++++++\n\n.. code-block:: python\n\n # acquire non-blocking lock\n lock1 = Lock('my_lock')\n lock2 = Lock('my_lock')\n\n # successfully acquire lock1\n lock1.acquire()\n\n # try to acquire lock in a non-blocking way\n lock2.acquire(False) == True # returns False\n\n # try to acquire lock in a blocking way\n lock2.acquire() # blocks until lock is acquired to timeout happens\n\nUsing two backends at the same time\n+++++++++++++++++++++++++++++++++++\n\nConfiguring Sherlock to use a backend does not limit you from using\nanother backend at the same time. You can import backend specific locks like\nRedisLock, MCLock and EtcdLock and use them just the same way you use a generic\nlock (see below). In fact, the generic Lock provided by Sherlock is just\na proxy that uses these specific locks under the hood.\n\n.. code-block:: python\n\n import sherlock\n from sherlock import Lock\n\n # Configure Sherlock's locks to use Redis as the backend\n sherlock.configure(backend=sherlock.backends.REDIS)\n\n # Acquire a lock called my_lock, this lock uses Redis\n lock = Lock('my_lock')\n\n # Now acquire locks in Memcached\n from sherlock import MCLock\n mclock = MCLock('my_mc_lock')\n mclock.acquire()\n\nTests\n-----\n\nTo run all the tests (including integration), you have to make sure that all\nthe databases are running. Make sure all the services are running:\n\n.. code:: bash\n\n # memcached\n memcached\n\n # redis-server\n redis-server\n\n # etcd (etcd is probably not available as package, here is the simplest way\n # to run it).\n wget https://github.com/coreos/etcd/releases/download/<version>/etcd-<version>-<platform>.tar.gz\n tar -zxvf etcd-<version>-<platform>.gz\n ./etcd-<version>-<platform>/etcd\n\nRun tests like so:\n\n.. code:: bash\n\n python setup.py test\n\nDocumentation\n-------------\n\nAvailable `here`_.\n\n.. _here: http://sher-lock.readthedocs.org\n\nLicense\n-------\n\nSee `LICENSE`_.\n\n**In short**: This is an open-source project and exists for anyone to use it\nand/or modify it for personal use. Just be nice and attribute the credits\nwherever you can. :)\n\n.. _LICENSE: http://github.com/vaidik/sherlock/blob/master/LICENSE.rst\n\nQuestions?\n----------\n\nYou are encouraged to ask questions using `issues`_ as that helps everyone and\nmyself when people with better know-how contribute to the discussion. However,\nif you wish to get in touch with me personally, then I can be contacted at\n**kapoor.vaidik++github+sherlock@gmail.com**.\n\n.. _issues: https://github.com/vaidik/sherlock/issues\n\nDistributed Locking in Other Languages\n--------------------------------------\n\n* NodeJS - https://github.com/thedeveloper/warlock\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Distributed inter-process locks with a choice of backend",
"version": "0.4.1",
"project_urls": {
"Homepage": "https://github.com/py-sherlock/sherlock",
"Repository": "https://github.com/py-sherlock/sherlock"
},
"split_keywords": [
"locking"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "dc7b75242a72cdfd228014d470f8404357e357c99c36ad44744133cd1fd07813",
"md5": "56f4c675f5555d021a33b5f7a97c1a39",
"sha256": "811f1f410b4be035726bf602d00c05d5969534d52158725cd3562ccadd2f7325"
},
"downloads": -1,
"filename": "sherlock-0.4.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "56f4c675f5555d021a33b5f7a97c1a39",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7,<4.0",
"size": 16443,
"upload_time": "2023-02-18T14:50:51",
"upload_time_iso_8601": "2023-02-18T14:50:51.623797Z",
"url": "https://files.pythonhosted.org/packages/dc/7b/75242a72cdfd228014d470f8404357e357c99c36ad44744133cd1fd07813/sherlock-0.4.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "173c4f44dbb806efdd86005b3ac183a6c9c27f361e3ef473e41f5f553758ea6f",
"md5": "60a80a1b677af01b7e17885c2a743846",
"sha256": "e620f498d5e2618005d3b5e08cc34106c1ab09e9c58d4310fc268b011700b6f5"
},
"downloads": -1,
"filename": "sherlock-0.4.1.tar.gz",
"has_sig": false,
"md5_digest": "60a80a1b677af01b7e17885c2a743846",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7,<4.0",
"size": 17012,
"upload_time": "2023-02-18T14:50:53",
"upload_time_iso_8601": "2023-02-18T14:50:53.850457Z",
"url": "https://files.pythonhosted.org/packages/17/3c/4f44dbb806efdd86005b3ac183a6c9c27f361e3ef473e41f5f553758ea6f/sherlock-0.4.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-02-18 14:50:53",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "py-sherlock",
"github_project": "sherlock",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "sherlock"
}