Skip to main content

A simple UUID-based file lock implementation for Python.

Project description

UUID File Lock

PyPi badge

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

  1. Lock File Check: The system checks if the lock file exists.
  2. Lock Creation: If no lock file exists, it creates one and writes a unique UUID to it.
  3. Verification: The lock waits briefly and then verifies if the UUID matches the one it wrote.
  4. Retry Logic: If the UUID mismatches or the lock file already exists, it retries after a delay.
  5. 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.

working principal

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.

License

This package is licensed under the MIT License.

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

uuid_filelock-1.3.post2.tar.gz (31.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

uuid_filelock-1.3.post2-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file uuid_filelock-1.3.post2.tar.gz.

File metadata

  • Download URL: uuid_filelock-1.3.post2.tar.gz
  • Upload date:
  • Size: 31.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for uuid_filelock-1.3.post2.tar.gz
Algorithm Hash digest
SHA256 ef78ca3e16ddc6fd864fdb2ab819f074e8210debdc1c11943d1749925c3a805e
MD5 f7f78718f082510ba348a500bf4b257c
BLAKE2b-256 cdfdc0ecc0bcea1df05c30645da1ffcf9d10600985587305bdc41b712614f8c7

See more details on using hashes here.

Provenance

The following attestation bundles were made for uuid_filelock-1.3.post2.tar.gz:

Publisher: python-publish.yml on z3rone-org/uuid_filelock

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file uuid_filelock-1.3.post2-py3-none-any.whl.

File metadata

File hashes

Hashes for uuid_filelock-1.3.post2-py3-none-any.whl
Algorithm Hash digest
SHA256 d4863b7dd5ad8b3537719974ce559f01e8d238ad9499417bea11a9ccc95eb76a
MD5 a259b2c10945724bea4f41c94192f7c9
BLAKE2b-256 53fc702b43deb9afcb2246de71a5cce8a352896d6f59fd1c52e3762e7760928e

See more details on using hashes here.

Provenance

The following attestation bundles were made for uuid_filelock-1.3.post2-py3-none-any.whl:

Publisher: python-publish.yml on z3rone-org/uuid_filelock

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page