python-filelock

Introduction

python-filelock is a cross-platform file locking library that provides OS-independent file locking mechanisms for Python applications. It supports various locking modes, suitable for inter-process synchronization, resource access control, and other scenarios.

Key Features

  • Cross-platform support (Windows, Linux, macOS)
  • Thread and process-safe file locking
  • Support for blocking and non-blocking locking modes
  • Automatic deadlock detection and recovery mechanisms
  • Timeout control functionality
  • Multiple lock types (hard lock, soft lock)
  • Async file lock support

Project Structure

python-filelock/
├── python-filelock.spec    # RPM packaging spec file
├── filelock-3.20.1.tar.gz  # Source package (managed via Git LFS)
├── src/filelock/           # Core source code directory
│   ├── __init__.py         # Module initialization
│   ├── _api.py             # API interface definition
│   ├── _error.py           # Exception definitions
│   ├── _soft.py            # Soft lock implementation
│   ├── _unix.py            # Unix platform implementation
│   ├── _util.py            # Utility functions
│   ├── _windows.py         # Windows platform implementation
│   ├── asyncio.py          # Async support
│   └── version.py          # Version information
└── tests/                  # Test cases
    ├── test_filelock.py
    ├── test_async_filelock.py
    └── ...

Quick Start

from filelock import FileLock

# Create a file lock
with FileLock("my_file.lock"):
    # Safely access resources within this block
    with open("my_file.txt", "r") as f:
        data = f.read()

Requirements

  • Python 3.10 or higher
  • Supports Python 3.10-3.14

Project Information