Typing stubs for filelock

3.2.7 · active · verified Tue Apr 14

This package provides static type checking stubs for the `filelock` library, enabling tools like MyPy to verify type correctness. It is part of the Python typeshed project, which maintains high-quality stubs generally kept up-to-date with the latest versions of their respective runtime libraries. Releases follow typeshed's continuous integration and release cycle, reflecting updates to the underlying `filelock` library.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the usage of the `filelock` library, for which `types-filelock` provides type hints. To run this code with type checking, ensure both `filelock` and `types-filelock` are installed. The `FileLock` context manager ensures that the enclosed code block is executed exclusively, preventing race conditions across processes.

import os
import time
from filelock import FileLock

# Define a lock file path
lock_file_path = "/tmp/my_app.lock"

try:
    # Acquire the lock. If another process holds it, this will block until it's released.
    # The timeout parameter ensures it doesn't wait indefinitely.
    with FileLock(lock_file_path, timeout=5):
        print("Lock acquired. Performing exclusive operation...")
        # Simulate work
        time.sleep(2)
        print("Exclusive operation completed. Releasing lock.")
except Exception as e:
    print(f"Could not acquire lock or an error occurred: {e}")

view raw JSON →