A simple file-based lock utility for Python applications
Project description
locktools
A file-based lock utility for Python applications and services. Useful for coordinating access to shared resources, pausing/resuming processes, and ensuring data consistency between distributed components.
Features
- JSON lock file with status, timestamp, service, flush status, and unique lock ID
- Acquire and release locks
- Update and monitor flush status
- Wait for flush completion with timeout
- Read lock file status as a dictionary
- Option to delete or empty the lock file on release
Installation
pip install locktools
Usage
from locktools import LockFile
# Create a lock file for a generic service
lock = LockFile(lockfile_path='lockfile.txt', service='myservice', timeout=10)
lock.acquire() # Acquire the lock
# Check if locked
if lock.is_locked():
print("Locked!")
# Update flush status (e.g., after a flush or checkpoint)
lock.update_flush_status('done')
# Wait for flush status to become 'done' (returns True if successful within timeout)
lock.wait_for_flush(expected_status='done')
# Read lock file status
status = lock.read_status()
print(status)
# Release the lock (default: empties the file)
lock.release()
# Optionally, delete the lock file on release
lock2 = LockFile(delete_on_release=True)
lock2.acquire()
lock2.release() # This will delete the file
Lock File JSON Example
{
"status": "locked",
"timestamp": "2024-06-10T12:34:56.789Z",
"service": "myservice",
"flush_status": "pending",
"lock_id": "uuid-1234-abcd"
}
License
MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
locktools-0.3.2.tar.gz
(4.3 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file locktools-0.3.2.tar.gz.
File metadata
- Download URL: locktools-0.3.2.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
28c8b215f7fa9e86d241c569f6efca1c1da33c48ec0c16148486769d102b6ced
|
|
| MD5 |
00f73f9c635cf07239dd37ab411db91e
|
|
| BLAKE2b-256 |
b01c17e258cef33ce5bd83e10186e40170fc03722d31783da3e16a2177822006
|
File details
Details for the file locktools-0.3.2-py3-none-any.whl.
File metadata
- Download URL: locktools-0.3.2-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a3f0a124810e64259b4bc5a1baf14fb0e5458d260b5f6d93dd3139c8bd8cf88f
|
|
| MD5 |
0cc7ff6d937b0c9d109bbfea1b7b5577
|
|
| BLAKE2b-256 |
ff2b32cc3161a1066d63c7bf5a32dc621f835f00c71c322dd3efa9b7b729350e
|