{"id":24784464,"url":"https://github.com/miintto/redis-lock-py","last_synced_at":"2025-10-12T08:30:29.978Z","repository":{"id":65821368,"uuid":"594731875","full_name":"miintto/redis-lock-py","owner":"miintto","description":"Redis distributed lock implementation for Python based on Pub/Sub messaging","archived":false,"fork":false,"pushed_at":"2024-11-03T11:16:06.000Z","size":33,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-03T11:49:23.747Z","etag":null,"topics":["asyncio","distributed-lock","pubsub-messages","python","redis"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/redis-lock-py","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/miintto.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"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":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-01-29T13:30:02.000Z","updated_at":"2024-12-20T07:27:02.000Z","dependencies_parsed_at":"2024-11-02T16:19:29.843Z","dependency_job_id":"f75c394b-cb5c-4112-9769-52ae55604fe9","html_url":"https://github.com/miintto/redis-lock-py","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miintto%2Fredis-lock-py","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miintto%2Fredis-lock-py/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miintto%2Fredis-lock-py/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miintto%2Fredis-lock-py/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/miintto","download_url":"https://codeload.github.com/miintto/redis-lock-py/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":236184444,"owners_count":19108674,"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":["asyncio","distributed-lock","pubsub-messages","python","redis"],"created_at":"2025-01-29T13:14:51.514Z","updated_at":"2025-10-12T08:30:29.640Z","avatar_url":"https://github.com/miintto.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![License](https://img.shields.io/badge/license-MIT-lightgray.svg)](./LICENSE)\n[![PyPI Release](https://img.shields.io/pypi/v/redis-lock-py)](https://pypi.org/project/redis-lock-py/)\n[![Downloads](https://static.pepy.tech/badge/redis-lock-py)](https://pepy.tech/project/redis-lock-py)\n![Python Support](https://img.shields.io/pypi/pyversions/redis-lock-py)\n![Implementation](https://img.shields.io/pypi/implementation/redis-lock-py.svg)\n[![codecov](https://codecov.io/gh/miintto/redis-lock-py/branch/master/graph/badge.svg?token=I9A9JKIWKF)](https://codecov.io/gh/miintto/redis-lock-py)\n\n# Redis Lock with PubSub\n\nRedis distributed lock implementation for Python based on Pub/Sub messaging.\n\n## 1. Features\n\n- Ensure atomicity by using the SETNX operation.\n- Implements a Pub/Sub messaging system between the client attempting to acquire the lock and the one currently holding it.\n- Includes a forced timeout mechanism to prevent infinite loops when attempting to acquire the lock.\n- Supports asynchronous operations.\n\n## 2. Installation\n\n```bash\n$\u003e pip install redis-lock-py\n```\n\n### Dependencies\n- Python \u003e= 3.9\n- redis-py \u003e= 5.2.0\n\n## 3. Usage\n\n### 3.1 Basic Example\n\n```python\nimport redis\nfrom redis_lock import RedisLock\n\nclient = redis.Redis(host=\"127.0.0.1\", port=6379)\n\nname = \"foo\"\nlock = RedisLock(client, name)\nif not lock.acquire():\n    raise Exception(\"Fail to acquire lock\")\nprint(\"Acquired lock successfully!\")\nlock.release()\n```\n\nThe [redis-py](https://github.com/redis/redis-py) library is required for Redis connection objects.\nAfter successfully acquiring the lock using `RedisLock.acquire`, ensure to release it by calling `RedisLock.release` to prevent lock retention.\n\n### 3.2 Using Context Managers\n\n```python\nimport redis\nfrom redis_lock import RedisLock\n\nclient = redis.Redis(host=\"127.0.0.1\", port=6379)\n\nwith RedisLock(client, name=\"foo\", blocking_timeout=10):\n    print(\"Acquired lock successfully!\")\n```\n\nTo avoid issues where the lock remains unreleased (potentially blocking other clients from acquiring it),\nyou can use `RedisLock` with a context manager, which ensures that the lock is automatically released at the end of the `with` block.\nBoth examples in sections **3.1** and **3.2** function in a same manner.\n\n### 3.3 With Asyncio\n\n```python\nfrom redis.asyncio import Redis\nfrom redis_lock.asyncio import RedisLock\n\nclient = Redis(host=\"127.0.0.1\", port=6379)\n\nasync with RedisLock(client, name=\"foo\", blocking_timeout=10):\n    print(\"Acquired lock successfully!\")\n```\n\n**redis-lock** supports the asyncio platform.\n\n### 3.4 Using Spin Lock\n\n```python\nimport redis\nfrom redis_lock import RedisSpinLock\n\nclient = redis.Redis(host=\"127.0.0.1\", port=6379)\n\nlock = RedisSpinLock(client, name=\"foo\")\nif not lock.acquire(blocking=True, sleep_time=0.1):\n    raise Exception(\"Fail to acquire lock\")\nprint(\"Acquired lock successfully!\")\nlock.release()\n```\n\nWhile a spin lock is available,\nit is not recommended unless there is a compelling reason to use it, as it is less efficient compared to the Pub/Sub messaging system.\n\n### System Flow\n\n![redis-lock-flow](https://user-images.githubusercontent.com/37063580/215324117-ff55fc4e-cc14-42c1-8628-e472adf8b865.png)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmiintto%2Fredis-lock-py","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmiintto%2Fredis-lock-py","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmiintto%2Fredis-lock-py/lists"}