Skip to main content

inotify for asyncio

Project description

inotipy is a high-level Python wrapper for the Linux
[`inotify(7)`](http://man7.org/linux/man-pages/man7/inotify.7.html) API,
that is designed to work with
[`asyncio`](https://docs.python.org/3/library/asyncio.html) event loops
in Python 3.5 or later. This lets you efficiently monitor one or more
files or directories for interesting happenings, without having to
continually poll the filesystem. But note, however, that if events are
coming too thick and fast, the kernel is liable to start dropping
them, which will be reported to you by events with the `IN.Q_OVERFLOW`
bit set.


Basic Usage
===========

Let us say there is a directory, initially empty, called “test”.
The following script monitors that for changes:

import sys
import asyncio
import inotipy

loop = asyncio.get_event_loop()

paths_to_watch = ["test"] # edit as necessary

async def mainline() :
watcher = inotipy.Watcher.create()
for path in paths_to_watch :
watcher.watch(path, inotipy.IN.ALL_EVENTS)
#end for
while True :
event = await watcher.get()
sys.stdout.write("Got event: %s\n" % repr(event))
#end while
#end mainline

loop.run_until_complete(mainline())

Suppose we start the script, and then in another terminal session
perform the following series of command-line actions:

$ echo hi >test/f1
$ mv test/f1 test/f2
$ cat test/f2
$ rm test/f2
$ rmdir test

then we should see the script report an event stream something like
this:

Got event: Event(1, [<EVENT_BIT.CREATE: 8>], 0, 'f1')
Got event: Event(1, [<EVENT_BIT.OPEN: 5>], 0, 'f1')
Got event: Event(1, [<EVENT_BIT.MODIFY: 1>], 0, 'f1')
Got event: Event(1, [<EVENT_BIT.CLOSE_WRITE: 3>], 0, 'f1')
Got event: Event(1, [<EVENT_BIT.MOVED_FROM: 6>], 217185, 'f1')
Got event: Event(1, [<EVENT_BIT.MOVED_TO: 7>], 217185, 'f2')
Got event: Event(1, [<EVENT_BIT.OPEN: 5>], 0, 'f2')
Got event: Event(1, [<EVENT_BIT.ACCESS: 0>], 0, 'f2')
Got event: Event(1, [<EVENT_BIT.CLOSE_NOWRITE: 4>], 0, 'f2')
Got event: Event(1, [<EVENT_BIT.DELETE: 9>], 0, 'f2')
Got event: Event(1, [<EVENT_BIT.DELETE_SELF: 10>], 0, '')
Got event: Event(1, [<EVENT_BIT.IGNORED: 15>], 0, '')

That should give you the basic flavour. For more elaborate examples,
see my `inotipy_examples` repo
([GitLab](https://gitlab.com/ldo/inotipy_examples),
[GitHub](https://github.com/ldo/inotipy_examples)).

There is more functionality available: see the `inotify(7)` man page
for more details, and of course the documentation in `inotify.py`
itself.

Lawrence D'Oliveiro <ldo@geek-central.gen.nz>
2018 March 11


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

inotipy-0.1.1-py3-none-any.whl (16.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page