Monitoring file changes with inotify

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 asyncio
import aionotify

# Setup the watcher
watcher = 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 loop
loop = asyncio.get_event_loop()

async def work():
await watcher.setup(loop)
for _ in range(10):
event = await watcher.get_event()
print(event)
watcher.close()

loop.run_until_complete(work())
loop.stop()
loop.close()
watcher.unwatch('/tmp/foo.txt')

Here, a watcher object is created. This class wraps the inotify_init function call. Then a watcher is added with the watch method of the Watcher class. The alias argument will be provided when events are emitted. This can be useful when several files are being watched with the same watcher object. The path of the file to watch is also provided, as well as the events that must be monitored. Here, four events will be notified when the file is opened, closed, read, and written respectively.

To test this application, an initial foo file must be present in the filesystem:

echo "bar" > /tmp/foo.txt

Then the application can be started:

(venv-rx) $ python3 ch8/inotify.py

To test it, open the file in a text editor and make some changes in it. The console of the application should display something similar to the following:

Event(flags=32, cookie=0, name='', alias='test')
Event(flags=1, cookie=0, name='', alias='test')
Event(flags=32, cookie=0, name='', alias='test')
Event(flags=2, cookie=0, name='', alias='test')
Event(flags=8, cookie=0, name='', alias='test')
Event(flags=32, cookie=0, name='', alias='test')
Event(flags=2, cookie=0, name='', alias='test')
Event(flags=8, cookie=0, name='', alias='test')

The flags correspond to the values of the aionotify.Flags enumeration. The value 32 is when the file is open. 1 is for access, 2 is for modification, and 8 is when the file is closed. Depending on the editor being used, different values will be printed, depending on what you type in the file and how the editor manages changes in the files. Here, my editor does the following actions each time I save the file: opens the file, makes some modifications, and then closes the file. Other editors keep the file open and write to it either on request from the user or automatically. There are also cases where there is literally a flood of access or modify events. This is something that can be easily handled with RxPY.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.91.47