Skip to main content

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.

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.

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

from uuid_filelock import UUIDFileLock

lock_file_path = "/tmp/mylockfile.lock"
lock = UUIDFileLock(lock_file_path)

try:
    print("Trying to acquire lock...")
    lock.acquire()
    print("Lock acquired!")

    # Perform critical section tasks here
    # Simulating work
    time.sleep(5)

finally:
    lock.release()
    print("Lock released.")

Example: Using the Lock in a with Clause

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.
  • retry_interval (float, default=1.0): Time to wait before retrying if the lock cannot be acquired.

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-0.3.tar.gz (4.3 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-0.3-py3-none-any.whl (2.9 kB view details)

Uploaded Python 3

File details

Details for the file uuid_filelock-0.3.tar.gz.

File metadata

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

File hashes

Hashes for uuid_filelock-0.3.tar.gz
Algorithm Hash digest
SHA256 7a390c38c2f7f2ce8c97bc96e1150cc84ba75dd0261a52a36dc3a3f63f700d70
MD5 4a77e9c7645218e3730e085a21ebf72e
BLAKE2b-256 00d5269be7d9e0a8b5cbf12fb1993e17d509887ebec94c236cd9b2ba0837f5e1

See more details on using hashes here.

Provenance

The following attestation bundles were made for uuid_filelock-0.3.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-0.3-py3-none-any.whl.

File metadata

  • Download URL: uuid_filelock-0.3-py3-none-any.whl
  • Upload date:
  • Size: 2.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for uuid_filelock-0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ab4721056dad18ecf019fa9e42539790c5b86e9890868192bc2b5b76b42f59e4
MD5 55bab883c34c0b8d7723b701e9004499
BLAKE2b-256 7cf51bfeb4c7d34fbdc0ccb8d2026e8f747a2459d2152a057c7d8cf416c2f30c

See more details on using hashes here.

Provenance

The following attestation bundles were made for uuid_filelock-0.3-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