lockable


Namelockable JSON
Version 0.12.0 PyPI version JSON
download
home_pagehttps://github.com/jupe/py-lockable
Summarylockable resource module
upload_time2025-08-20 05:57:41
maintainerNone
docs_urlNone
authorJussi Vatjus-Anttila
requires_python<4,>=3.8
licenseNone
keywords py.test pytest lockable resource
VCS
bugtrack_url
requirements coverage httptest mock pid pydash requests setuptools-scm urllib3 setuptools
Travis-CI No Travis.
coveralls test coverage
            # lockable

[![CircleCI](https://circleci.com/gh/jupe/py-lockable/tree/master.svg?style=svg)](https://circleci.com/gh/jupe/py-lockable/tree/master)
[![PyPI version](https://badge.fury.io/py/lockable.svg)](https://pypi.org/project/lockable/)
[![Coverage Status](https://coveralls.io/repos/github/jupe/pytest-lockable/badge.svg)](https://coveralls.io/github/jupe/py-lockable)

Resource locking module for python.

Originally designed for following projects:
* [pytest-lockable](https://github.com/jupe/pytest-lockable)
* [robot-lockable](https://github.com/jupe/robot-lockable)


Module provides python API and simple CLI interface.

Resource is released in following cases:
* process ends
* when context ends when `lockable.auto_lock(..)` is used
* allocation.unlock() is called
* lockable.unlock(<allocation>) is called

Resources data provider support following mechanisms:
* `resources.json` file in file system
* python list of dictionaries
* http uri which points to API and is used with HTTP GET method. API should provide `resources.json` data as json object.

# CLI interface

```
% lockable --help
usage: lockable [-h] [--validate-only] [--lock-folder LOCK_FOLDER] [--resources RESOURCES]
                [--timeout TIMEOUT] [--hostname HOSTNAME]
                [--requirements REQUIREMENTS]
                [command [command ...]]

run given command while suitable resource is allocated.
Usage example: lockable --requirements {"online":true} echo using resource: $ID

positional arguments:
  command               Command to be execute during device allocation

optional arguments:
  -h, --help            show this help message and exit
  --validate-only       Only validate resources.json
  --lock-folder LOCK_FOLDER
                        lock folder
  --resources RESOURCES
                        Resources file (utf-8) or http uri
  --timeout TIMEOUT     Timeout for trying allocate suitable resource
  --hostname HOSTNAME   Hostname
  --requirements REQUIREMENTS
                        requirements as json string

```

# API's

Constructor
```python
lockable = Lockable([hostname], [resource_list_file], [resource_list], [lock_folder])
```

Allocation
```python
allocation_context = lockable.lock(requirements, [timeout_s])
print(allocation_context.resource_info)
print(allocation_context.resource_id)
allocation_context.unlock()
# or using resource info
lockable.unlock(allocation_context)
```

Allocation context contains following API:
* `requirements: dict` Original requirements for allocation
* `resource_info: dict` Allocated resource information
* `unlock(): func`  release resource lock function
* `allocation_queue_time: timedelta` How long waited before allocation
* `allocation_start_time: datetime` when allocation was started
* `release_time: datetime` when allocation was ended
* `alloc_id: str` allocation id
* `allocation_durations: timedelta` how long time allocation takes

or using context manager which unlock automatically
```python
with lockable.auto_lock(requirements, [timeout_s]) as allocation:
    print(allocation.resource_info)
```

**Tips:**

You can allocate also offline devices by set requirements `"online": None` . 
You can ignore also `hostname` same same way by  setting it to  None`



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/jupe/py-lockable",
    "name": "lockable",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4,>=3.8",
    "maintainer_email": null,
    "keywords": "py.test pytest lockable resource",
    "author": "Jussi Vatjus-Anttila",
    "author_email": "jussiva@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/3b/8a/b0beeae525f38182c3cecfb41f75d1db4ca7340c5c4eb1c7cc6ca7f63fb1/lockable-0.12.0.tar.gz",
    "platform": null,
    "description": "# lockable\n\n[![CircleCI](https://circleci.com/gh/jupe/py-lockable/tree/master.svg?style=svg)](https://circleci.com/gh/jupe/py-lockable/tree/master)\n[![PyPI version](https://badge.fury.io/py/lockable.svg)](https://pypi.org/project/lockable/)\n[![Coverage Status](https://coveralls.io/repos/github/jupe/pytest-lockable/badge.svg)](https://coveralls.io/github/jupe/py-lockable)\n\nResource locking module for python.\n\nOriginally designed for following projects:\n* [pytest-lockable](https://github.com/jupe/pytest-lockable)\n* [robot-lockable](https://github.com/jupe/robot-lockable)\n\n\nModule provides python API and simple CLI interface.\n\nResource is released in following cases:\n* process ends\n* when context ends when `lockable.auto_lock(..)` is used\n* allocation.unlock() is called\n* lockable.unlock(<allocation>) is called\n\nResources data provider support following mechanisms:\n* `resources.json` file in file system\n* python list of dictionaries\n* http uri which points to API and is used with HTTP GET method. API should provide `resources.json` data as json object.\n\n# CLI interface\n\n```\n% lockable --help\nusage: lockable [-h] [--validate-only] [--lock-folder LOCK_FOLDER] [--resources RESOURCES]\n                [--timeout TIMEOUT] [--hostname HOSTNAME]\n                [--requirements REQUIREMENTS]\n                [command [command ...]]\n\nrun given command while suitable resource is allocated.\nUsage example: lockable --requirements {\"online\":true} echo using resource: $ID\n\npositional arguments:\n  command               Command to be execute during device allocation\n\noptional arguments:\n  -h, --help            show this help message and exit\n  --validate-only       Only validate resources.json\n  --lock-folder LOCK_FOLDER\n                        lock folder\n  --resources RESOURCES\n                        Resources file (utf-8) or http uri\n  --timeout TIMEOUT     Timeout for trying allocate suitable resource\n  --hostname HOSTNAME   Hostname\n  --requirements REQUIREMENTS\n                        requirements as json string\n\n```\n\n# API's\n\nConstructor\n```python\nlockable = Lockable([hostname], [resource_list_file], [resource_list], [lock_folder])\n```\n\nAllocation\n```python\nallocation_context = lockable.lock(requirements, [timeout_s])\nprint(allocation_context.resource_info)\nprint(allocation_context.resource_id)\nallocation_context.unlock()\n# or using resource info\nlockable.unlock(allocation_context)\n```\n\nAllocation context contains following API:\n* `requirements: dict` Original requirements for allocation\n* `resource_info: dict` Allocated resource information\n* `unlock(): func`  release resource lock function\n* `allocation_queue_time: timedelta` How long waited before allocation\n* `allocation_start_time: datetime` when allocation was started\n* `release_time: datetime` when allocation was ended\n* `alloc_id: str` allocation id\n* `allocation_durations: timedelta` how long time allocation takes\n\nor using context manager which unlock automatically\n```python\nwith lockable.auto_lock(requirements, [timeout_s]) as allocation:\n    print(allocation.resource_info)\n```\n\n**Tips:**\n\nYou can allocate also offline devices by set requirements `\"online\": None` . \nYou can ignore also `hostname` same same way by  setting it to  None`\n\n\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "lockable resource module",
    "version": "0.12.0",
    "project_urls": {
        "Bug Reports": "https://github.com/jupe/pytest-lockable",
        "Homepage": "https://github.com/jupe/py-lockable",
        "Source": "https://github.com/jupe/pytest-lockable/"
    },
    "split_keywords": [
        "py.test",
        "pytest",
        "lockable",
        "resource"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "ebca33558cd3b7480be2eff9d9a765f9d9c3d2da4a1d4b8ee3d53580f3249ca0",
                "md5": "1c36f4aaa606727f669daaef4c3c3f78",
                "sha256": "5c7c0eaa0b93c1e2f233380799e32415f3425215710f908e532f159533340b12"
            },
            "downloads": -1,
            "filename": "lockable-0.12.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "1c36f4aaa606727f669daaef4c3c3f78",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4,>=3.8",
            "size": 13757,
            "upload_time": "2025-08-20T05:57:40",
            "upload_time_iso_8601": "2025-08-20T05:57:40.325949Z",
            "url": "https://files.pythonhosted.org/packages/eb/ca/33558cd3b7480be2eff9d9a765f9d9c3d2da4a1d4b8ee3d53580f3249ca0/lockable-0.12.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "3b8ab0beeae525f38182c3cecfb41f75d1db4ca7340c5c4eb1c7cc6ca7f63fb1",
                "md5": "5c7a5cafcdf0cd7e430cf379964f1180",
                "sha256": "7cd3aeb756c7af31659166e1b40a03c184513ea2ce933a7a198cb2b049bb226e"
            },
            "downloads": -1,
            "filename": "lockable-0.12.0.tar.gz",
            "has_sig": false,
            "md5_digest": "5c7a5cafcdf0cd7e430cf379964f1180",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4,>=3.8",
            "size": 24395,
            "upload_time": "2025-08-20T05:57:41",
            "upload_time_iso_8601": "2025-08-20T05:57:41.477256Z",
            "url": "https://files.pythonhosted.org/packages/3b/8a/b0beeae525f38182c3cecfb41f75d1db4ca7340c5c4eb1c7cc6ca7f63fb1/lockable-0.12.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-08-20 05:57:41",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "jupe",
    "github_project": "py-lockable",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": true,
    "circle": true,
    "requirements": [
        {
            "name": "coverage",
            "specs": [
                [
                    "==",
                    "7.4.0"
                ]
            ]
        },
        {
            "name": "httptest",
            "specs": [
                [
                    "==",
                    "2.1.1"
                ]
            ]
        },
        {
            "name": "mock",
            "specs": [
                [
                    "==",
                    "5.1.0"
                ]
            ]
        },
        {
            "name": "pid",
            "specs": [
                [
                    "==",
                    "3.0.4"
                ]
            ]
        },
        {
            "name": "pydash",
            "specs": [
                [
                    "==",
                    "7.0.6"
                ]
            ]
        },
        {
            "name": "requests",
            "specs": [
                [
                    "==",
                    "2.31.0"
                ]
            ]
        },
        {
            "name": "setuptools-scm",
            "specs": [
                [
                    "==",
                    "8.0.4"
                ]
            ]
        },
        {
            "name": "urllib3",
            "specs": [
                [
                    "==",
                    "2.1.0"
                ]
            ]
        },
        {
            "name": "setuptools",
            "specs": [
                [
                    "~=",
                    "69.0.3"
                ]
            ]
        }
    ],
    "lcname": "lockable"
}
        
Elapsed time: 1.39418s