lockfile (deprecated PyPI package)

0.12.2 · deprecated · verified Sun Mar 29

The `lockfile` package (version 0.12.2) provides a platform-independent API for locking files across Unix and Windows, relying on atomic system calls like `link` (Unix) and `mkdir` (Windows). This package is **deprecated**, with its last release in November 2015. Users are strongly advised to use alternatives like `fasteners` or `oslo.concurrency` for file locking needs.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to acquire and release a file lock using `LockFile`. It includes a timeout mechanism and proper error handling for when the lock cannot be acquired or is already held. The `os.environ.get` is used for the lock file path for potential external configuration.

from lockfile import LockFile
import os

lock_path = os.environ.get('LOCK_FILE_PATH', 'my_app.lock')
lock = LockFile(lock_path)

try:
    print(f"Attempting to acquire lock for {lock.path}...")
    lock.acquire(timeout=10) # Wait up to 10 seconds
    print(f"Lock acquired for {lock.path}. Performing critical operation...")
    # Simulate work
    with open('shared_resource.txt', 'a') as f:
        f.write('Critical operation performed.\n')
    print("Critical operation complete.")
except lock.AlreadyLocked: # Using lock.AlreadyLocked for consistency across versions
    print(f"Could not acquire lock for {lock.path}: Already locked by another process.")
except Exception as e:
    print(f"An error occurred: {e}")
finally:
    if lock.is_locked():
        lock.release()
        print(f"Lock released for {lock.path}.")
    else:
        print(f"Lock was not acquired, so no release needed for {lock.path}.")

view raw JSON →