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
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:
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
Linuxadvisory locking mechanismflock. - 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
flockto the lock file and- if successful write the process pid into the pid file
- if
flockfailed 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
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}")
Metadata
Release files for resource-lock 0.6.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| resource_lock-0.6.0.tar.gz | 5.2 kB | Without provenance | Details for resource_lock-0.6.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| resource_lock-0.6.0-py3-none-any.whl | Python 3 | none | any | Details for resource_lock-0.6.0-py3-none-any.whl |
Total release size: 10.0 kB
Release files / resource_lock-0.6.0.tar.gz
| Download URL | resource_lock-0.6.0.tar.gz |
|---|---|
| Size | 5.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
460952cb768946c36fd405129531c2d3a615156a3af2b42dbadd5c2021922ea5
|
|
BLAKE2b-256 checksum How to use checksums |
f60a265878f2bc61e2d1a9c3cbca0a1a20512fcd27104a6c557981db8845d48f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.12.4
|
Release files / resource_lock-0.6.0-py3-none-any.whl
| Download URL | resource_lock-0.6.0-py3-none-any.whl |
|---|---|
| Size | 4.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b52ef41a3b3e56c0af32c2a7fe037b8466a3307bb35e1361608d744032481d64
|
|
BLAKE2b-256 checksum How to use checksums |
9e7f30f05f4372bd5469d2cd9a564d7cb7cb1d0dbdd9ddabb8d82b24a72ee426
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.12.4
|
Security for resource-lock 0.6.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.