watchdog-gevent is a Python library that provides a gevent-based observer for the `watchdog` file system monitoring library. It allows `watchdog` to function efficiently within a gevent cooperative multitasking environment, replacing the default threading-based observer. The current version is 0.2.1, with recent updates in late 2024, indicating an active but low-cadence maintenance.
pip install watchdog-geventVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use `watchdog-gevent` to monitor a directory. It's crucial to apply `gevent.monkey.patch_all()` at the very beginning of your script, before importing other modules that might rely on blocking I/O or threading, to ensure the gevent observer functions correctly within the cooperative multitasking environment. The example creates a temporary directory, sets up an observer, and prints messages for file creation, deletion, and modification events.
Ensure `import gevent.monkey; gevent.monkey.patch_all()` is the first significant code executed in your application, before any other I/O or threading-related imports.
Implement custom logic using `on_created` and `on_deleted` events to infer moves, or consider using the standard `watchdog` observers if `gevent` compatibility for move events is not a strict requirement.
Offload CPU-bound tasks to separate processes or threads using `gevent.threadpool`, or ensure CPU-intensive operations are non-blocking. File I/O, even with monkey-patching, can be blocking on some operating systems; use `gevent.threadpool` for concurrent file I/O.
Use Python 3.9 or newer. Consult the documentation for `watchdog` and `gevent` for their current Python version requirements.