October 2018
Intermediate to advanced
420 pages
10h 26m
English
Fortunately, there is already a Python package that exposes the inotify features with Asyncio—aionotify. This package is available on pypi and can be installed with pip (as usual from your virtualenv):
pip3 install aionotify
The aionotify API is simple to use (by the way, the original inotify API is already simple for a C API), and access to the events leverages coroutines and the await notation. See the following example:
import asyncioimport aionotify# Setup the watcherwatcher = aionotify.Watcher()watcher.watch(alias='test', path='/tmp/foo.txt', flags= aionotify.Flags.OPEN | aionotify.Flags.CLOSE_WRITE | aionotify.Flags.ACCESS | aionotify.Flags.MODIFY)# Prepare the looploop = asyncio.get_event_loop() ...