pie-lock


Namepie-lock JSON
Version 0.1.7 PyPI version JSON
download
home_pagehttps://pypi.org/project/pie-lock
SummaryA library for python distributed lock, optimistic lock and limiter
upload_time2023-01-12 08:21:35
maintainerTuanDC
docs_urlNone
authorTuanDC
requires_python>=3.6.2,<4.0.0
license
keywords distributed lock lock redis lock limiter
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Pie-lock

<p align="center">
    <em>All lock module  using redis for control.</em>
</p>
<a href="https://pypi.org/project/pie-lock" target="_blank">
    <img src="https://img.shields.io/pypi/v/fastapi?color=%2334D058&label=pypi%20package" alt="Package version">
</a>
<a href="https://pypi.org/project/pie-lock" target="_blank">
    <img src="https://img.shields.io/pypi/pyversions/fastapi.svg?color=%2334D058" alt="Supported Python versions">
</a>

## Installation
With Pypi:
``` bash
pip install pie-lock
```

With Github:
``` bash
pip install git+https://github.com/bacsiTuan/pielock.git
```

## Usage Distributed Lock
``` python
key = "test1"
success, msg = redis_lock.acquire(key)
print(msg)
if not success:
    print(msg)
redis_lock.release(key)
```

## Usage Optimistic Lock
``` python
def test_optimistic_lock(self):
    is_locked1, msg = redis_lock.acquire("key1")
    if not is_locked1:
        print(msg)
    is_locked2, msg = redis_lock.acquire("key1")
    if not is_locked2:
        print(msg)
    is_locked3, msg = redis_lock.acquire("key1")
    if not is_locked3:
        print(msg)
    release, msg = redis_lock.release("key1")
    if not release:
        print(msg)
    is_locked4, msg = redis_lock.acquire("key1")
    if not is_locked4:
        print(msg)
```
## Configuration

Redis configuration
``` python
from pie_lock.backends import DistributedLock

redis_lock = DistributedLock(
    expires=5,
    timeout=5,
    retry_after=1, # seconds between retries
    tries=32,  # max number of tries
)
redis_lock.get_client(
    host="localhost",
    port=19821,
    password="passsword",
    username="default"
)
```

Note: all fields after the scheme are optional, and will default to
localhost on port 6379, using database 0.


``DEFAULT_TIMEOUT`` (default: 60)

If another client has already obtained the lock, sleep for a maximum of
this many seconds before giving up. A value of 0 means no wait (give up
right away).

The default timeout can be overridden when instantiating the lock.

## Limiter
Based on sliding window algorithm
``` python
from pie_lock.backends import Limiter, TimeUnit

redis = Limiter(
    host="localhost",
    port=19821,
    password="passsword",
    username="default",
    socket_timeout=2,
)

for i in range(6):
    allow, msg = redis.allow(redis_key="mylist", per=TimeUnit.SECOND, count=2)
    if not allow:
        print(msg)
time.sleep(1)
allow, msg = redis.allow(redis_key="mylist", per=TimeUnit.SECOND, count=2)
if not allow:
    print(msg)
```

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

We consider any existing lock older than this many seconds to be invalid
in order to detect crashed clients. This value must be higher than it
takes the critical section to execute.

The default expires can be overridden when instantiating the lock.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

            

Raw data

            {
    "_id": null,
    "home_page": "https://pypi.org/project/pie-lock",
    "name": "pie-lock",
    "maintainer": "TuanDC",
    "docs_url": null,
    "requires_python": ">=3.6.2,<4.0.0",
    "maintainer_email": "tuandao864@gmail.com",
    "keywords": "distributed lock,lock,redis lock,limiter",
    "author": "TuanDC",
    "author_email": "tuandao864@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/4e/28/ab26c1a552ac68f65564087e2bb758e29bfc70a7903612bb7b94c9ec07ae/pie_lock-0.1.7.tar.gz",
    "platform": null,
    "description": "# Pie-lock\n\n<p align=\"center\">\n    <em>All lock module  using redis for control.</em>\n</p>\n<a href=\"https://pypi.org/project/pie-lock\" target=\"_blank\">\n    <img src=\"https://img.shields.io/pypi/v/fastapi?color=%2334D058&label=pypi%20package\" alt=\"Package version\">\n</a>\n<a href=\"https://pypi.org/project/pie-lock\" target=\"_blank\">\n    <img src=\"https://img.shields.io/pypi/pyversions/fastapi.svg?color=%2334D058\" alt=\"Supported Python versions\">\n</a>\n\n## Installation\nWith Pypi:\n``` bash\npip install pie-lock\n```\n\nWith Github:\n``` bash\npip install git+https://github.com/bacsiTuan/pielock.git\n```\n\n## Usage Distributed Lock\n``` python\nkey = \"test1\"\nsuccess, msg = redis_lock.acquire(key)\nprint(msg)\nif not success:\n    print(msg)\nredis_lock.release(key)\n```\n\n## Usage Optimistic Lock\n``` python\ndef test_optimistic_lock(self):\n    is_locked1, msg = redis_lock.acquire(\"key1\")\n    if not is_locked1:\n        print(msg)\n    is_locked2, msg = redis_lock.acquire(\"key1\")\n    if not is_locked2:\n        print(msg)\n    is_locked3, msg = redis_lock.acquire(\"key1\")\n    if not is_locked3:\n        print(msg)\n    release, msg = redis_lock.release(\"key1\")\n    if not release:\n        print(msg)\n    is_locked4, msg = redis_lock.acquire(\"key1\")\n    if not is_locked4:\n        print(msg)\n```\n## Configuration\n\nRedis configuration\n``` python\nfrom pie_lock.backends import DistributedLock\n\nredis_lock = DistributedLock(\n    expires=5,\n    timeout=5,\n    retry_after=1, # seconds between retries\n    tries=32,  # max number of tries\n)\nredis_lock.get_client(\n    host=\"localhost\",\n    port=19821,\n    password=\"passsword\",\n    username=\"default\"\n)\n```\n\nNote: all fields after the scheme are optional, and will default to\nlocalhost on port 6379, using database 0.\n\n\n``DEFAULT_TIMEOUT`` (default: 60)\n\nIf another client has already obtained the lock, sleep for a maximum of\nthis many seconds before giving up. A value of 0 means no wait (give up\nright away).\n\nThe default timeout can be overridden when instantiating the lock.\n\n## Limiter\nBased on sliding window algorithm\n``` python\nfrom pie_lock.backends import Limiter, TimeUnit\n\nredis = Limiter(\n    host=\"localhost\",\n    port=19821,\n    password=\"passsword\",\n    username=\"default\",\n    socket_timeout=2,\n)\n\nfor i in range(6):\n    allow, msg = redis.allow(redis_key=\"mylist\", per=TimeUnit.SECOND, count=2)\n    if not allow:\n        print(msg)\ntime.sleep(1)\nallow, msg = redis.allow(redis_key=\"mylist\", per=TimeUnit.SECOND, count=2)\nif not allow:\n    print(msg)\n```\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nWe consider any existing lock older than this many seconds to be invalid\nin order to detect crashed clients. This value must be higher than it\ntakes the critical section to execute.\n\nThe default expires can be overridden when instantiating the lock.\n\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "A library for python distributed lock, optimistic lock and limiter",
    "version": "0.1.7",
    "split_keywords": [
        "distributed lock",
        "lock",
        "redis lock",
        "limiter"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8f0a2a4ef1517bbd2cc1fcdcff30c3db91a4d9c7258aca87f6a67343b6fd7266",
                "md5": "46766d18aa6c772df89e04a406f7a5b4",
                "sha256": "91207bfb4695563b9710d54cf681b34248e6574587d36660fce92a070cf44b9d"
            },
            "downloads": -1,
            "filename": "pie_lock-0.1.7-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "46766d18aa6c772df89e04a406f7a5b4",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.6.2,<4.0.0",
            "size": 12351,
            "upload_time": "2023-01-12T08:21:33",
            "upload_time_iso_8601": "2023-01-12T08:21:33.618668Z",
            "url": "https://files.pythonhosted.org/packages/8f/0a/2a4ef1517bbd2cc1fcdcff30c3db91a4d9c7258aca87f6a67343b6fd7266/pie_lock-0.1.7-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4e28ab26c1a552ac68f65564087e2bb758e29bfc70a7903612bb7b94c9ec07ae",
                "md5": "0a070a914e8f48159f20cc395b76dbd5",
                "sha256": "cb4b13570d7bb6b2d9cb61b930ec9c6040f6e68d85a3c5edf25f86795b50d582"
            },
            "downloads": -1,
            "filename": "pie_lock-0.1.7.tar.gz",
            "has_sig": false,
            "md5_digest": "0a070a914e8f48159f20cc395b76dbd5",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6.2,<4.0.0",
            "size": 10458,
            "upload_time": "2023-01-12T08:21:35",
            "upload_time_iso_8601": "2023-01-12T08:21:35.803156Z",
            "url": "https://files.pythonhosted.org/packages/4e/28/ab26c1a552ac68f65564087e2bb758e29bfc70a7903612bb7b94c9ec07ae/pie_lock-0.1.7.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-01-12 08:21:35",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "lcname": "pie-lock"
}
        
Elapsed time: 0.03059s