The zc.lockfile package provides a basic, portable implementation of interprocess locks using file-locking primitives. It is not specifically for locking files themselves, but rather for providing general-purpose interprocess locks with a file-based mechanism. Currently at version 4.0, the library is actively maintained with updates primarily focused on Python version compatibility and bug fixes.
pip install zc.lockfileVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to acquire and release an inter-process lock using `zc.lockfile.LockFile`. It includes proper error handling for when a lock cannot be acquired and ensures the lock is released in a `finally` block to prevent deadlocks. It also shows how to optionally remove the lock file after use.
Update code to use new-style class patterns and explicitly handle lock file content if custom writing behavior was implicitly relied upon.
Ensure your packaging environment and tools are compatible with PEP 420 native namespaces. Direct `from zc.lockfile import LockFile` imports should generally be unaffected.
Always ensure `lock.close()` is called, typically within a `try...finally` block, to guarantee the lock is released. `zc.lockfile.LockFile` does not implement the context manager protocol, so a `with` statement cannot be used directly.
If lock files should not persist, add an `os.remove(lock_file_path)` call after `lock.close()` (and potentially wrap it in a `try...except OSError` block for robustness).
Leverage the `zc.lockfile` feature to include hostname (which often corresponds to container ID) in the lock file to aid in identifying the lock's origin in distributed or containerized setups.
pip install zc.lockfile
Ensure locks are always released using a 'with' statement or 'try...finally' block, or manually remove stale lock files if certain they are not in use. Example: `with LockFile('my.lock'): # critical section`Use 'LockFile' instead of 'Lock'. Example: `from zc.lockfile import LockFile`
Change the lock file's path to a directory where the process has write permissions, or adjust the file system permissions for the target directory.
No dependency data recorded yet.