inotify-simple is a lightweight Python wrapper around the Linux `inotify` API, implemented using `ctypes`. It provides a direct, low-level interface to filesystem events without much abstraction, making it efficient and close to the kernel's behavior. The library is currently at version 2.0.1 and is actively maintained with a stable release cadence.
pip install inotify-simpleVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to set up a basic `inotify` watch on a directory for creation, deletion, modification, and close-write events. It then continuously reads and prints detected events. It also shows how to gracefully stop the monitoring and clean up resources.
Implement logic to recursively add watches for new and existing subdirectories. Continuously scan for new directories if full recursive monitoring is required.
Set a `timeout` argument in `INotify.read()` or wrap the `INotify` instance with `select.select()` or an `asyncio` event loop to handle concurrent I/O operations.
Increase kernel limits (`sysctl -w fs.inotify.max_queued_events=...`). Design event processing to be as fast as possible to minimize queue backlog, or implement a recovery strategy (e.g., rescan monitored directories) if an overflow event (`flags.Q_OVERFLOW`) is received.
Maintain a dictionary mapping watch descriptors (`wd`) to full paths. When processing events, use the `wd` to retrieve the most current path from your cache, and be prepared for the `name` field to be outdated or point to a non-existent file.
If user/process attribution or network filesystem monitoring is required, `inotify-simple` is not the right tool. Consider auditing solutions or higher-level network file system APIs.
Ensure 'inotify-simple' is installed (`pip install inotify-simple`) and use the correct import statement for the library's main components, typically `from inotify_simple import INotify, flags`.
Increase the kernel parameters `fs.inotify.max_user_watches`, `fs.inotify.max_user_instances`, and `fs.inotify.max_queued_events` by modifying `/etc/sysctl.conf` and reloading with `sudo sysctl -p`.
Verify that the `path` exists before attempting to add a watch. For dynamic scenarios involving deletions, catch the `OSError` and handle it gracefully, or consider watching the parent directory for creation events instead of specific files.
Ensure that the standard C library (typically `libc.so.6` or similar) is correctly installed and discoverable on your Linux system. On Debian/Ubuntu-based systems, installing `libc6-dev` often resolves this issue.