{"id":895,"library":"lockfile","title":"lockfile (deprecated PyPI package)","description":"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.","status":"deprecated","version":"0.12.2","language":"python","source_language":"en","source_url":"http://git.openstack.org/cgit/openstack/pylockfile","tags":["file-locking","concurrency","inter-process","deprecated"],"install":[{"cmd":"pip install lockfile","lang":"bash","label":"Install deprecated version"}],"dependencies":[],"imports":[{"note":"While 'import lockfile' works, accessing LockFile directly from the top-level package is the common pattern.","wrong":"import lockfile","symbol":"LockFile","correct":"from lockfile import LockFile"},{"note":"As of version 0.9, classes like LinkFileLock were moved into submodules, though the old top-level import was retained for backward compatibility until 1.0.","wrong":"from lockfile import LinkFileLock","symbol":"LinkFileLock","correct":"from lockfile.linklockfile import LinkFileLock"},{"note":"Similar to LinkFileLock, MkdirFileLock was moved to a submodule in version 0.9.","wrong":"from lockfile import MkdirFileLock","symbol":"MkdirFileLock","correct":"from lockfile.mkdirlockfile import MkdirFileLock"}],"quickstart":{"code":"from lockfile import LockFile\nimport os\n\nlock_path = os.environ.get('LOCK_FILE_PATH', 'my_app.lock')\nlock = LockFile(lock_path)\n\ntry:\n    print(f\"Attempting to acquire lock for {lock.path}...\")\n    lock.acquire(timeout=10) # Wait up to 10 seconds\n    print(f\"Lock acquired for {lock.path}. Performing critical operation...\")\n    # Simulate work\n    with open('shared_resource.txt', 'a') as f:\n        f.write('Critical operation performed.\\n')\n    print(\"Critical operation complete.\")\nexcept lock.AlreadyLocked: # Using lock.AlreadyLocked for consistency across versions\n    print(f\"Could not acquire lock for {lock.path}: Already locked by another process.\")\nexcept Exception as e:\n    print(f\"An error occurred: {e}\")\nfinally:\n    if lock.is_locked():\n        lock.release()\n        print(f\"Lock released for {lock.path}.\")\n    else:\n        print(f\"Lock was not acquired, so no release needed for {lock.path}.\")","lang":"python","description":"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."},"warnings":[{"fix":"Update import paths and class names to reflect the new structure (e.g., `from lockfile.linklockfile import LinkLockFile`). For backward compatibility, the old module-level definitions were retained until the 1.0 release, but direct submodule imports are recommended.","message":"The API underwent significant changes in version 0.9. Classes like `LinkFileLock`, `MkdirFileLock`, and `SQLiteFileLock` were moved from the top-level `lockfile` module into their own submodules (e.g., `lockfile.linklockfile.LinkFileLock`). The class naming convention also reversed, changing from `SomethingFileLock` to `SomethingLockFile`.","severity":"breaking","affected_versions":"0.9.x to 0.12.2"},{"fix":"Migrate to modern, actively maintained file locking libraries such as `fasteners` or `oslo.concurrency`.","message":"This `lockfile` package is officially deprecated and has not been updated since November 2015. It is highly recommended to migrate to actively maintained alternatives.","severity":"deprecated","affected_versions":"All versions, especially 0.12.2 and earlier"},{"fix":"For applications requiring robust locking over network file systems, consider alternatives explicitly designed for such environments or more advanced coordination primitives. For instance, `flufl.lock` is noted as NFS-safe for POSIX systems.","message":"The `LockFile` implementation relies on the atomic nature of `link()` on Unix and `mkdir()` on Windows. While providing cross-platform compatibility, this mechanism might not be suitable for all network file systems (e.g., NFS), where atomic guarantees can be an issue.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure `release()` is only called when the lock is known to be held. Using the `with` statement for `LockFile` instances is the safest approach, as it automatically handles acquisition and release, reducing the chance of `LockError` from manual management. If manual control is necessary, check `lock.is_locked()` before calling `release()`.","message":"Calling `release()` on an already unlocked `LockFile` instance will raise a `LockError`. This can occur if the lock is released prematurely or if there's a logic error in handling lock states, particularly in complex multi-process scenarios or when mixing explicit `acquire`/`release` with context managers.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"search_vec":"'0.12.2':9 '2015':44 'across':19 'advis':48 'altern':51 'api':15 'atom':25 'call':27 'concurr':63 'deprec':2,37,67 'fasten':53 'file':18,57,61 'file-lock':60 'independ':14 'inter':65 'inter-process':64 'last':40 'like':28,52 'link':29 'lock':17,58,62 'lockfil':1,6 'mkdir':32 'need':59 'novemb':43 'oslo.concurrency':55 'packag':4,7,35 'platform':13 'platform-independ':12 'process':66 'provid':10 'pypi':3 'releas':41 'reli':23 'strong':47 'system':26 'unix':20,30 'use':50 'user':45 'version':8 'window':22,33","created_at":"2026-03-29T06:06:38.914935+00:00","updated_at":"2026-04-16T16:20:44.193599+00:00","problems":[{"fix":"Run `pip install lockfile` or `pip3 install lockfile`. Note that the `lockfile` package is deprecated; consider using alternatives like `fasteners` or `oslo.concurrency` for new projects.","cause":"The 'lockfile' package is not installed in the current Python environment.","error":"ModuleNotFoundError: No module named 'lockfile'"},{"fix":"Ensure `lockfile` is installed via `pip install lockfile`. If the error persists, try reinstalling the dependent package (e.g., `python-daemon`) or check its compatibility requirements. It is highly recommended to migrate to actively maintained libraries like `fasteners` or `oslo.concurrency`.","cause":"This error typically occurs when another package, such as `python-daemon`, expects a specific sub-module (`pidlockfile`) from `lockfile` that is either not installed, or there is an incompatibility with the versions of the dependent package and `lockfile`.","error":"ImportError: No module named lockfile.pidlockfile"},{"fix":"You can either increase the `timeout` value when calling `acquire()`, implement a retry mechanism, or cautiously use `lock.break_lock()` if you are certain the existing lock is stale. For robust solutions, consider migrating to `fasteners` or `oslo.concurrency`.","cause":"The `acquire()` method was called with a specified timeout, but the lock could not be obtained within that duration because another process or thread was holding the lock.","error":"lockfile.LockTimeout: Lock held by another process"},{"fix":"Handle the `AlreadyLocked` exception in your code, or call `acquire()` without a timeout for a blocking operation, or provide a positive timeout value. It is advisable to use modern, maintained alternatives like `fasteners` or `oslo.concurrency`.","cause":"This exception is raised when `lockfile.acquire()` is called in a non-blocking mode (e.g., with `timeout=0` or a negative value), and the file is already locked by another process or thread.","error":"lockfile.AlreadyLocked: File is already locked"},{"fix":"Check for other processes that might be holding a lock on the target file or directory and terminate them if necessary. Verify that the Python process has appropriate read/write permissions for the directory where the lock file is created. For better reliability and features, consider switching to `fasteners` or `oslo.concurrency`.","cause":"A low-level operating system error occurred (e.g., 'Resource temporarily unavailable', errno 11) preventing `lockfile` from creating or acquiring the lock. This often indicates another process holds a conflicting lock, or there are insufficient file system permissions.","error":"IO error: Could not lock file"}],"ecosystem":"pypi","meta_description":null,"install_score":100,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":"0.12.2","cli_name":"","cli_version":null,"type":"library","homepage":"http://launchpad.net/pylockfile","github":null,"docs":null,"changelog":null,"pypi":"https://pypi.org/project/lockfile/","npm":null,"openapi_spec":null,"status_page":null,"smithery":null,"categories":["database"],"base_url":null,"auth_type":null,"provenance":{"verified_status":"passing","verified_at":"2026-06-28","last_verified":"2026-06-28","next_check":"2026-07-28","install_tag":"verified"}}