Typing stubs for filelock
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
- gotcha Installing `types-filelock` does NOT install the `filelock` runtime library. You must install `filelock` separately (e.g., `pip install filelock`) for your code to function.
- gotcha Do not attempt to import symbols directly from `types_filelock` (or any variation of the stub package name). The purpose of `types-filelock` is to provide type information for imports from the original `filelock` library.
- gotcha Type stubs are maintained by the typeshed project and may occasionally lag behind the absolute latest releases of the `filelock` library. This can lead to `mypy` errors if `filelock` introduces new features or changes its API before corresponding stubs are updated.
Install
-
pip install types-filelock
Imports
- FileLock
from filelock import FileLock
Quickstart
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}")