Skip to main content

lockable

CI PyPI version Coverage Status

Resource locking module for python.

Originally designed for following projects:

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() 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

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

Allocation

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

with lockable.auto_lock(requirements, [timeout_s]) as allocation:
    print(allocation.resource_info)

Resource requirements are evaluated using mongoquery, so MongoDB-style operators like $in and $gt are supported when selecting resources.

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`

Metadata

Release files for lockable 0.13.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lockable 0.13.1
File Size Uploaded
lockable-0.13.1.tar.gz 24.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for lockable 0.13.1
File Interpreter ABI Platform
lockable-0.13.1-py3-none-any.whl Python 3 none any Details

Total release size: 38.8 kB

Release files / lockable-0.13.1.tar.gz

Download URL lockable-0.13.1.tar.gz
Size 24.7 kB
Tags Source
SHA-256 checksum
How to use checksums
fe5e28d297f4ac1c115fef7d6f68a933a9ea5e15d1e9dd44d8faeeae03de6d56
BLAKE2b-256 checksum
How to use checksums
d9da137bf2874789b6f6adb1d4c4d6dbcdb3b5b1cbabb3e83e684102d4781c11
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.11

Release files / lockable-0.13.1-py3-none-any.whl

Download URL lockable-0.13.1-py3-none-any.whl
Size 14.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dc1d21f8a18ad9c90d5a7d3ff8d1113540f507467bceae3c4b133cc9191c724d
BLAKE2b-256 checksum
How to use checksums
900c7707ff3b532aabfcfb073efd4f978ed2e7734104df833cb0bf90d87b555c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.11

Release history Release notifications | RSS feed

This release

0.13.1 This release

2 release files

0.12.0

2 release files

0.11.1

2 release files

0.11.0

2 release files

0.9.0

2 release files

0.8.0

2 release files

0.7.2

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page