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 details)

Uploaded Python 3

File details

Details for the file inotipy-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: inotipy-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 16.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0

File hashes

Hashes for inotipy-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 825a26cd669047f49f56ce708bbe3b04b662710bce54cdf29a76f3a0ad40c988
MD5 ab2f12d0b605df298eaa2af700d97075
BLAKE2b-256 f80dc5ab325342bcc9fccf65a4619ded6c15db44b2292cada703afe7e9ccaa28

See more details on using hashes here.

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