Portalocker is a Python library that provides an easy-to-use API for cross-platform file locking. It supports file locking on Windows, Linux, BSD, and Unix systems, and can also facilitate distributed locking using Redis. The library is actively maintained, follows Semantic Versioning, and provides a convenient context manager for lock management.
pip install portalockerVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates the recommended way to use `portalocker` with the `Lock` context manager to safely acquire and release file locks, ensuring data integrity across processes. It explicitly shows flushing and syncing the file content to disk.
Ensure all processes accessing the file use `portalocker` or coordinate access through other means. Be aware of the advisory nature on POSIX systems.
Use modes 'a' or 'a+' with `Lock(..., truncate=0)` to avoid premature truncation, or use `portalocker.utils.open_atomic` for atomic file writes. For example: `with portalocker.Lock('somefile', mode='a', truncate=0) as fh:`Always call `fh.flush()` followed by `os.fsync(fh.fileno())` after writing critical data to ensure it's written to disk within the locked section.
Upgrade to Python 3 (recommended) or pin `portalocker` to a version less than 2.0 (`pip install "portalocker<2"`).
To resolve this, you can either implement a retry mechanism for acquiring the lock with a suitable timeout, or configure `portalocker.Lock` to wait by setting an appropriate `timeout` parameter (which defaults to `None` for infinite wait, but can be set to a float for a specific duration). If `fail_when_locked=True` (the default for `Lock`), you might catch `portalocker.exceptions.AlreadyLocked` for immediate failure, or set `fail_when_locked=False` to make it block until timeout.
Verify that your Redis server is running and accessible from the machine where your Python application is executing. Ensure that the host and port specified when initializing `portalocker.RedisLock` (or the underlying Redis connection) correctly match your Redis server's configuration. For example: `import portalocker; import redis; client = redis.Redis(host='your_redis_host', port=6379); lock = portalocker.RedisLock('my_channel', connection=client)`.Grant the user running the Python script the appropriate read and write permissions for the directory where `portalocker` attempts to create its lock files, and for the file being locked itself. This often involves using `chmod` and `chown` commands on Unix-like systems or adjusting security settings on Windows.
First, ensure `portalocker` is correctly installed in your current Python environment using `pip install portalocker`. If you are working with older Python versions, consider upgrading your Python interpreter to 3.7+ or installing a compatible older version of `portalocker` (e.g., `pip install 'portalocker<2'` for Python 2.x environments if absolutely necessary). Verify that your Python interpreter is pointing to the correct environment where the package is installed.
pip install portalocker