A simple UUID-based file lock implementation for Python.
Project description
UUID File Lock
A simple UUID-based file lock implementation for Python. This package provides a lightweight and effective way to coordinate access to shared resources using a lock file mechanism. This does not require any file locking capabilities of the underlying filesystem or network share.
How It Works
- Lock File Check: The system checks if the lock file exists.
- Lock Creation: If no lock file exists, it creates one and writes a unique UUID to it.
- Verification: The lock waits briefly and then verifies if the UUID matches the one it wrote.
- Retry Logic: If the UUID mismatches or the lock file already exists, it retries after a delay.
- Release Mechanism: When the lock is released, the lock file is deleted if it contains the same UUID.
This mechanism ensures that only one process can hold the lock at any time, making it ideal for coordinating file or resource access across multiple processes.
Note: In order for this to work over a network share the verification_delay needs to be larger than the time it takes for a client to detect that another has created a file.
Installation
Install the package via pip:
pip install uuid-filelock
Usage
You can use the UUIDFileLock class to acquire and release locks in your Python code. The class also supports usage within a with clause for convenience.
Example: Using the Lock Explicitly
import time
from uuid_filelock import UUIDFileLock
lock_file_path = "/tmp/mylockfile.lock"
lock = UUIDFileLock(lock_file_path)
print("Acquire lock...")
lock.acquire()
print("Lock acquired!")
# Perform critical section tasks here
# Simulating work
time.sleep(5)
# Release the lock
lock.release()
print("Lock released.")
Example: Using the Lock in a with Clause
import time
from uuid_filelock import UUIDFileLock
lock_file_path = "/tmp/mylockfile.lock"
with UUIDFileLock(lock_file_path) as lock:
print("Lock acquired!")
# Perform critical section tasks
time.sleep(5) # Simulate work
print("Work done!")
# The lock is automatically released when the block exits.
print("Lock released.")
Parameters
lock_file(str): The path to the lock file.prefix(str, default=""): A custom prefix for the UUID to help debugging.verification_delay(float, default=1.0): Time to wait before verifying that the lock was acquired.retry_interval(float, default=0.1): Time to wait before retrying if the lock cannot be acquired.timeout_interval(float, default=-1): Timeout for acquiring lock. Set to negative value for no timeout.
Change Default Values
You can change the default values for verification_delay and retry_interval
via UUIDFileLock.verification_delay=<new_value> and UUIDFileLock.retry_interval=<new_value> respectively.
License
This package is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
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 uuid_filelock-1.4.tar.gz.
File metadata
- Download URL: uuid_filelock-1.4.tar.gz
- Upload date:
- Size: 32.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
882c5da6ab3f8a2e314e57e91a9046dcfca08676cdb8bfe0c105d4c888b75b06
|
|
| MD5 |
8765e8fe6274f79e1a86a05e9ff661c8
|
|
| BLAKE2b-256 |
94e96102544a20f0d8091e86b563595e38b52e45eeb29dce7659c8c87e51f911
|
Provenance
The following attestation bundles were made for uuid_filelock-1.4.tar.gz:
Publisher:
python-publish.yml on z3rone-org/uuid_filelock
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
uuid_filelock-1.4.tar.gz -
Subject digest:
882c5da6ab3f8a2e314e57e91a9046dcfca08676cdb8bfe0c105d4c888b75b06 - Sigstore transparency entry: 160027067
- Sigstore integration time:
-
Permalink:
z3rone-org/uuid_filelock@7efa0ee4862ae1b1ad150254535653037189cd14 -
Branch / Tag:
refs/tags/v1.4 - Owner: https://github.com/z3rone-org
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@7efa0ee4862ae1b1ad150254535653037189cd14 -
Trigger Event:
release
-
Statement type:
File details
Details for the file uuid_filelock-1.4-py3-none-any.whl.
File metadata
- Download URL: uuid_filelock-1.4-py3-none-any.whl
- Upload date:
- Size: 3.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6bc8ae73ddb8a9233a6a72cd30dd8a3242ca184abc9cc1e44767626b0baeb383
|
|
| MD5 |
f8e5776aa7bdbe79c4005147ac9042a4
|
|
| BLAKE2b-256 |
4d5d71a731ccbe2ae41464baa45df0aab81a19a404ca69bbec73000bbf411463
|
Provenance
The following attestation bundles were made for uuid_filelock-1.4-py3-none-any.whl:
Publisher:
python-publish.yml on z3rone-org/uuid_filelock
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
uuid_filelock-1.4-py3-none-any.whl -
Subject digest:
6bc8ae73ddb8a9233a6a72cd30dd8a3242ca184abc9cc1e44767626b0baeb383 - Sigstore transparency entry: 160027069
- Sigstore integration time:
-
Permalink:
z3rone-org/uuid_filelock@7efa0ee4862ae1b1ad150254535653037189cd14 -
Branch / Tag:
refs/tags/v1.4 - Owner: https://github.com/z3rone-org
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@7efa0ee4862ae1b1ad150254535653037189cd14 -
Trigger Event:
release
-
Statement type: