{"id":13813931,"url":"https://github.com/ionelmc/python-redis-lock","last_synced_at":"2025-05-14T12:10:12.760Z","repository":{"id":11395202,"uuid":"13839795","full_name":"ionelmc/python-redis-lock","owner":"ionelmc","description":"Lock context manager implemented via redis SET NX EX and BLPOP.","archived":false,"fork":false,"pushed_at":"2024-07-03T14:21:30.000Z","size":612,"stargazers_count":559,"open_issues_count":29,"forks_count":81,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-04-11T04:58:34.435Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://pypi.python.org/pypi/python-redis-lock","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ionelmc.png","metadata":{"files":{"readme":"README.rst","changelog":"CHANGELOG.rst","contributing":"CONTRIBUTING.rst","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":"AUTHORS.rst","dei":null,"publiccode":null,"codemeta":null}},"created_at":"2013-10-24T17:57:29.000Z","updated_at":"2025-03-31T16:10:54.000Z","dependencies_parsed_at":"2024-06-18T12:34:15.586Z","dependency_job_id":"bcf1d206-58be-4628-8d87-085dcb6b82a7","html_url":"https://github.com/ionelmc/python-redis-lock","commit_stats":{"total_commits":291,"total_committers":22,"mean_commits":"13.227272727272727","dds":"0.22680412371134018","last_synced_commit":"149701266b4d147d6321875f19f604e9bacf4f4f"},"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ionelmc%2Fpython-redis-lock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ionelmc%2Fpython-redis-lock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ionelmc%2Fpython-redis-lock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ionelmc%2Fpython-redis-lock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ionelmc","download_url":"https://codeload.github.com/ionelmc/python-redis-lock/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254140749,"owners_count":22021219,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-04T04:01:36.985Z","updated_at":"2025-05-14T12:10:12.695Z","avatar_url":"https://github.com/ionelmc.png","language":"Python","readme":"========\nOverview\n========\n\n.. start-badges\n\n.. list-table::\n    :stub-columns: 1\n\n    * - docs\n      - |docs|\n    * - tests\n      - | |github-actions| |requires|\n        | |coveralls| |codecov|\n    * - package\n      - | |version| |wheel| |supported-versions| |supported-implementations|\n        | |commits-since|\n.. |docs| image:: https://readthedocs.org/projects/python-redis-lock/badge/?style=flat\n    :target: https://python-redis-lock.readthedocs.io/\n    :alt: Documentation Status\n\n.. |github-actions| image:: https://github.com/ionelmc/python-redis-lock/actions/workflows/github-actions.yml/badge.svg\n    :alt: GitHub Actions Build Status\n    :target: https://github.com/ionelmc/python-redis-lock/actions\n\n.. |requires| image:: https://requires.io/github/ionelmc/python-redis-lock/requirements.svg?branch=master\n    :alt: Requirements Status\n    :target: https://requires.io/github/ionelmc/python-redis-lock/requirements/?branch=master\n\n.. |coveralls| image:: https://coveralls.io/repos/ionelmc/python-redis-lock/badge.svg?branch=master\u0026service=github\n    :alt: Coverage Status\n    :target: https://coveralls.io/r/ionelmc/python-redis-lock\n\n.. |codecov| image:: https://codecov.io/gh/ionelmc/python-redis-lock/branch/master/graphs/badge.svg?branch=master\n    :alt: Coverage Status\n    :target: https://codecov.io/github/ionelmc/python-redis-lock\n\n.. |version| image:: https://img.shields.io/pypi/v/python-redis-lock.svg\n    :alt: PyPI Package latest release\n    :target: https://pypi.org/project/python-redis-lock\n\n.. |wheel| image:: https://img.shields.io/pypi/wheel/python-redis-lock.svg\n    :alt: PyPI Wheel\n    :target: https://pypi.org/project/python-redis-lock\n\n.. |supported-versions| image:: https://img.shields.io/pypi/pyversions/python-redis-lock.svg\n    :alt: Supported versions\n    :target: https://pypi.org/project/python-redis-lock\n\n.. |supported-implementations| image:: https://img.shields.io/pypi/implementation/python-redis-lock.svg\n    :alt: Supported implementations\n    :target: https://pypi.org/project/python-redis-lock\n\n.. |commits-since| image:: https://img.shields.io/github/commits-since/ionelmc/python-redis-lock/v4.0.0.svg\n    :alt: Commits since latest release\n    :target: https://github.com/ionelmc/python-redis-lock/compare/v4.0.0...master\n\n\n\n.. end-badges\n\nLock context manager implemented via redis SETNX/BLPOP.\n\n* Free software: BSD 2-Clause License\n\nInterface targeted to be exactly like `threading.Lock \u003chttps://docs.python.org/2/library/threading.html#threading.Lock\u003e`_.\n\nUsage\n=====\n\nBecause we don't want to require users to share the lock instance across processes you will have to give them names.\n\n.. code-block:: python\n\n    from redis import Redis\n    conn = Redis()\n\n    import redis_lock\n    lock = redis_lock.Lock(conn, \"name-of-the-lock\")\n    if lock.acquire(blocking=False):\n        print(\"Got the lock.\")\n        lock.release()\n    else:\n        print(\"Someone else has the lock.\")\n\nLocks as Context Managers\n=========================\n\n.. code-block:: python\n\n    conn = StrictRedis()\n    with redis_lock.Lock(conn, \"name-of-the-lock\"):\n        print(\"Got the lock. Doing some work ...\")\n        time.sleep(5)\n\n\nYou can also associate an identifier along with the lock so that it can be retrieved later by the same process, or by a\ndifferent one. This is useful in cases where the application needs to identify the lock owner (find out who currently\nowns the lock).\n\n.. code-block:: python\n\n    import socket\n    host_id = \"owned-by-%s\" % socket.gethostname()\n    lock = redis_lock.Lock(conn, \"name-of-the-lock\", id=host_id)\n    if lock.acquire(blocking=False):\n        assert lock.locked() is True\n        print(\"Got the lock.\")\n        lock.release()\n    else:\n        if lock.get_owner_id() == host_id:\n            print(\"I already acquired this in another process.\")\n        else:\n            print(\"The lock is held on another machine.\")\n\n\nAvoid dogpile effect in django\n------------------------------\n\nThe dogpile is also known as the thundering herd effect or cache stampede. Here's a pattern to avoid the problem\nwithout serving stale data. The work will be performed a single time and every client will wait for the fresh data.\n\nTo use this you will need `django-redis \u003chttps://github.com/jazzband/django-redis\u003e`_, however, ``python-redis-lock``\nprovides you a cache backend that has a cache method for your convenience. Just install ``python-redis-lock`` like\nthis:\n\n.. code-block:: bash\n\n    pip install \"python-redis-lock[django]\"\n\nNow put something like this in your settings:\n\n.. code-block:: python\n\n    CACHES = {\n        'default': {\n            'BACKEND': 'redis_lock.django_cache.RedisCache',\n            'LOCATION': 'redis://127.0.0.1:6379/1',\n            'OPTIONS': {\n                'CLIENT_CLASS': 'django_redis.client.DefaultClient'\n            }\n        }\n    }\n\n.. note::\n    If using a `django-redis` \u003c `3.8.x`, you'll probably need `redis_cache`\n    which has been deprecated in favor to `django_redis`. The `redis_cache`\n    module is removed in `django-redis` versions \u003e `3.9.x`. See `django-redis notes \u003chttps://github.com/jazzband/django-redis#configure-as-cache-backend\u003e`_.\n\n\nThis backend just adds a convenient ``.lock(name, expire=None)`` function to django-redis's cache backend.\n\nYou would write your functions like this:\n\n.. code-block:: python\n\n    from django.core.cache import cache\n\n    def function():\n        val = cache.get(key)\n        if not val:\n            with cache.lock(key):\n                val = cache.get(key)\n                if not val:\n                    # DO EXPENSIVE WORK\n                    val = ...\n                    cache.set(key, value)\n        return val\n\nTroubleshooting\n---------------\n\nIn some cases, the lock remains in redis forever (like a server blackout / redis or application crash / an unhandled\nexception). In such cases, the lock is not removed by restarting the application. One solution is to turn on the\n`auto_renewal` parameter in combination with `expire` to set a time-out on the lock, but let `Lock()` automatically\nkeep resetting the expire time while your application code is executing:\n\n.. code-block:: python\n\n    # Get a lock with a 60-second lifetime but keep renewing it automatically\n    # to ensure the lock is held for as long as the Python process is running.\n    with redis_lock.Lock(conn, name='my-lock', expire=60, auto_renewal=True):\n        # Do work....\n\nAnother solution is to use the ``reset_all()`` function when the application starts:\n\n.. code-block:: python\n\n    # On application start/restart\n    import redis_lock\n    redis_lock.reset_all()\n\nAlternatively, you can reset individual locks via the ``reset`` method.\n\nUse these carefully, if you understand what you do.\n\n\nFeatures\n========\n\n* based on the standard SETNX recipe\n* optional expiry\n* optional timeout\n* optional lock renewal (use a low expire but keep the lock active)\n* no spinloops at acquire\n\nImplementation\n==============\n\n``redis_lock`` will use 2 keys for each lock named ``\u003cname\u003e``:\n\n* ``lock:\u003cname\u003e`` - a string value for the actual lock\n* ``lock-signal:\u003cname\u003e`` - a list value for signaling the waiters when the lock is released\n\nThis is how it works:\n\n.. image:: https://raw.githubusercontent.com/ionelmc/python-redis-lock/master/docs/redis-lock%20diagram%20(v3.0).png\n    :alt: python-redis-lock flow diagram\n\nDocumentation\n=============\n\nhttps://python-redis-lock.readthedocs.io/en/latest/\n\nDevelopment\n===========\n\nTo run the all tests run::\n\n    tox\n\nRequirements\n============\n\n:OS: Any\n:Runtime: Python 2.7, 3.3 or later, or PyPy\n:Services: Redis 2.6.12 or later.\n\nSimilar projects\n================\n\n* `bbangert/retools \u003chttps://github.com/bbangert/retools/blob/0.4/retools/lock.py\u003e`_ - acquire does spinloop\n* `distributing-locking-python-and-redis \u003chttps://chris-lamb.co.uk/posts/distributing-locking-python-and-redis\u003e`_ - acquire does polling\n* `cezarsa/redis_lock \u003chttps://github.com/cezarsa/redis_lock/blob/0.2.0/redis_lock/__init__.py\u003e`_ - acquire does not block\n* `andymccurdy/redis-py \u003chttps://github.com/andymccurdy/redis-py/blob/3.5.3/redis/lock.py\u003e`_ - acquire does spinloop\n* `mpessas/python-redis-lock \u003chttps://github.com/mpessas/python-redis-lock/blob/b512eef0fc5e1e2e82a6a31f65cd88c2c37dfe4b/redislock/lock.py\u003e`_ - blocks fine but no expiration\n* `brainix/pottery \u003chttps://github.com/brainix/pottery/blob/v1.1.5/pottery/redlock.py\u003e`_ - acquire does spinloop\n","funding_links":[],"categories":["Python"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fionelmc%2Fpython-redis-lock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fionelmc%2Fpython-redis-lock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fionelmc%2Fpython-redis-lock/lists"}