resource-lock


Nameresource-lock JSON
Version 0.6.0 PyPI version JSON
download
home_pagehttps://github.com/robertblackwell/resource_lock
SummaryPython module to lock an arbitary resource
upload_time2024-08-03 01:39:45
maintainerNone
docs_urlNone
authorrobertblackwell
requires_pythonNone
licenseBSD 2-clause
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Pid File

This repo contains a small python module that is intended to allow multiple instances of a program
to lock a resource and in the event that the resurce is already locked to report who has locked the resource.

# Installing 

I have recently added the `resource_lock` project to __pypi__. As a result the module can be installed with

```bash
cd to your project folder
source venv/bin/activate
python3 -m pip install resource_lock
```

# Installing - backdoor way

Without using __pypi__ the simplest way to install this module into a project is:

```bash

cd .../Projects/my_project
cd ..
git clone git@github.com:robertblackwell/resource_lock.git
cd ../my_project
source ./env/bin/activate
pip install ../resource_lock

```

Where yout python project that wants to use `resource_lock` resides in `.../Projects/my_project` 

The repo `github.com:robertblackwell/resource_lock_example` demonstrates this

# Usage

Consider a situation where an application wishes to perform an action on an external resource. For example
sending a configuration file to an external device. 

The following points define the situations:

-   there may be multiple instances of the application running at any time for multiple users.

-   only one instance of the application is permitted to try sending to the device at the same time
as there is no locking mechanism in the remote device.

Thus this external device is a resource that cannot be shared.

A mechanism is required to ensure this device is "locked" while one instance of the app sends the
file. 

The basic mechansim is as follows:

-   use the `Linux` advisory locking mechanism `flock`.
-   provide a globally unique name for the resource to be protected `resource_name`
-   provide a path to a directory in which a lock file and pid file will be created
-   derive the name of the lock file as `f"{resource_name}.lock"`
-   derive the name of the pid file as `f"{resource_name}.pid"`
-   apply `flock` to the lock file and 
    -   if successful write the process  pid into the pid file
    -   if `flock` failed read the pid of the holder of the lock from the pid file and use that pid to get the user name of the lock holder
  
Then the following code segment demonstrates how to use this module

```python

from resource_lock import ResourceLock
# or from resource_lock.resource_lock import ResourceLock if using version lower than 0.4.0

resource_name = "....."
lockfile_dir_path = "....."

lock = ResourceLock(resource_name, lockfile_dir_path)

token = lock.acquire():
if token is not None:
    # the lock is successfully acquired
    send_file_to_remote_device(.....)
    # or whatever other stuff you need to do here

    lock.release(token)
else:
    # failed to acquire lock - print an error message

    print(f"Failed to acquire lock details are : {lock.error_msg}")


```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/robertblackwell/resource_lock",
    "name": "resource-lock",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": null,
    "author": "robertblackwell",
    "author_email": null,
    "download_url": "https://files.pythonhosted.org/packages/f6/0a/265878f2bc61e2d1a9c3cbca0a1a20512fcd27104a6c557981db8845d48f/resource_lock-0.6.0.tar.gz",
    "platform": null,
    "description": "# Pid File\n\nThis repo contains a small python module that is intended to allow multiple instances of a program\nto lock a resource and in the event that the resurce is already locked to report who has locked the resource.\n\n# Installing \n\nI have recently added the `resource_lock` project to __pypi__. As a result the module can be installed with\n\n```bash\ncd to your project folder\nsource venv/bin/activate\npython3 -m pip install resource_lock\n```\n\n# Installing - backdoor way\n\nWithout using __pypi__ the simplest way to install this module into a project is:\n\n```bash\n\ncd .../Projects/my_project\ncd ..\ngit clone git@github.com:robertblackwell/resource_lock.git\ncd ../my_project\nsource ./env/bin/activate\npip install ../resource_lock\n\n```\n\nWhere yout python project that wants to use `resource_lock` resides in `.../Projects/my_project` \n\nThe repo `github.com:robertblackwell/resource_lock_example` demonstrates this\n\n# Usage\n\nConsider a situation where an application wishes to perform an action on an external resource. For example\nsending a configuration file to an external device. \n\nThe following points define the situations:\n\n-   there may be multiple instances of the application running at any time for multiple users.\n\n-   only one instance of the application is permitted to try sending to the device at the same time\nas there is no locking mechanism in the remote device.\n\nThus this external device is a resource that cannot be shared.\n\nA mechanism is required to ensure this device is \"locked\" while one instance of the app sends the\nfile. \n\nThe basic mechansim is as follows:\n\n-   use the `Linux` advisory locking mechanism `flock`.\n-   provide a globally unique name for the resource to be protected `resource_name`\n-   provide a path to a directory in which a lock file and pid file will be created\n-   derive the name of the lock file as `f\"{resource_name}.lock\"`\n-   derive the name of the pid file as `f\"{resource_name}.pid\"`\n-   apply `flock` to the lock file and \n    -   if successful write the process  pid into the pid file\n    -   if `flock` failed read the pid of the holder of the lock from the pid file and use that pid to get the user name of the lock holder\n  \nThen the following code segment demonstrates how to use this module\n\n```python\n\nfrom resource_lock import ResourceLock\n# or from resource_lock.resource_lock import ResourceLock if using version lower than 0.4.0\n\nresource_name = \".....\"\nlockfile_dir_path = \".....\"\n\nlock = ResourceLock(resource_name, lockfile_dir_path)\n\ntoken = lock.acquire():\nif token is not None:\n    # the lock is successfully acquired\n    send_file_to_remote_device(.....)\n    # or whatever other stuff you need to do here\n\n    lock.release(token)\nelse:\n    # failed to acquire lock - print an error message\n\n    print(f\"Failed to acquire lock details are : {lock.error_msg}\")\n\n\n```\n",
    "bugtrack_url": null,
    "license": "BSD 2-clause",
    "summary": "Python module to lock an arbitary resource",
    "version": "0.6.0",
    "project_urls": {
        "Homepage": "https://github.com/robertblackwell/resource_lock"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "9e7f30f05f4372bd5469d2cd9a564d7cb7cb1d0dbdd9ddabb8d82b24a72ee426",
                "md5": "b0f46f3272bde4ca1dd6f4253eed7e66",
                "sha256": "b52ef41a3b3e56c0af32c2a7fe037b8466a3307bb35e1361608d744032481d64"
            },
            "downloads": -1,
            "filename": "resource_lock-0.6.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b0f46f3272bde4ca1dd6f4253eed7e66",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 4816,
            "upload_time": "2024-08-03T01:39:44",
            "upload_time_iso_8601": "2024-08-03T01:39:44.123014Z",
            "url": "https://files.pythonhosted.org/packages/9e/7f/30f05f4372bd5469d2cd9a564d7cb7cb1d0dbdd9ddabb8d82b24a72ee426/resource_lock-0.6.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f60a265878f2bc61e2d1a9c3cbca0a1a20512fcd27104a6c557981db8845d48f",
                "md5": "54059664693b1d040c2b47e0fbb1c6fe",
                "sha256": "460952cb768946c36fd405129531c2d3a615156a3af2b42dbadd5c2021922ea5"
            },
            "downloads": -1,
            "filename": "resource_lock-0.6.0.tar.gz",
            "has_sig": false,
            "md5_digest": "54059664693b1d040c2b47e0fbb1c6fe",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 5186,
            "upload_time": "2024-08-03T01:39:45",
            "upload_time_iso_8601": "2024-08-03T01:39:45.458228Z",
            "url": "https://files.pythonhosted.org/packages/f6/0a/265878f2bc61e2d1a9c3cbca0a1a20512fcd27104a6c557981db8845d48f/resource_lock-0.6.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-08-03 01:39:45",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "robertblackwell",
    "github_project": "resource_lock",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "resource-lock"
}
        
Elapsed time: 0.44304s