filelock
A platform independent file lock.
This package has a good security score with no known vulnerabilities.
Community Reviews
Rock-solid file locking with minimal fuss and excellent cross-platform support
The timeout parameter works reliably, and the library raises clear `Timeout` exceptions when locks can't be acquired. Type hints are present and work well with mypy and IDE autocompletion. The documentation is straightforward with practical examples that cover the common use cases like database file locking and preventing concurrent script execution.
One minor annoyance is that lock files aren't automatically cleaned up on process crashes (by design for safety), so you may need to implement your own stale lock detection for long-running systems. The error messages could be slightly more informative about which process holds a lock, but this is a minor quibble for an otherwise excellent library.
Best for: Applications needing reliable cross-platform file locking with minimal configuration, especially for preventing concurrent access to shared files or databases.
Avoid if: You need distributed locking across network filesystems (use Redis or database-based locks instead) or require automatic stale lock cleanup.
Simple, reliable file locking with minimal fuss
The library is remarkably low-maintenance once integrated. Error handling is predictable: you get `Timeout` exceptions when locks can't be acquired, and the messages clearly indicate which lock file is involved. Type hints are present and work well with mypy and IDE autocomplete. The documentation is concise but covers the essential use cases, including timeout configuration and non-blocking attempts.
One gotcha: lock files persist on disk after use, which can clutter directories if you're not careful about placement. The library also assumes you understand file locking semantics—it won't prevent you from common mistakes like acquiring multiple locks in inconsistent order (deadlock risk). For complex distributed locking scenarios, you'll need something more sophisticated, but for local file coordination, it's solid.
Best for: Local process coordination and preventing concurrent file access in scripts, CLI tools, or single-machine applications.
Avoid if: You need distributed locking across multiple machines or require advanced features like lock queuing and priority.
Solid, lightweight file locking with sensible defaults and cross-platform support
The timeout behavior is predictable and configurable. Default blocking behavior waits indefinitely, but you can set explicit timeouts that raise Timeout exceptions cleanly. The library doesn't do any hidden retries - you get exactly the behavior you configure. Resource cleanup is automatic with context managers, and I've never seen leaked lock files cause issues in production. Logging is minimal but sufficient; it uses the standard logging module so you can hook into it if needed.
Performance overhead is negligible - it's literally just OS-level file locking syscalls. Memory footprint is tiny. My only real gripe is that error messages when locks fail could be more descriptive about what's holding the lock, but that's an OS limitation more than a library issue.
Best for: Coordinating access to shared resources across processes on a single machine with straightforward locking needs.
Avoid if: You need distributed locking across multiple machines or require advanced features like lock introspection and deadlock detection.
Sign in to write a review
Sign In