Skip to main content

'A simple optionally-async python inotify library, focused on simplicity of use and operation, and leveraging modern Python features

Project description

An async python inotify package. Kept as simple and easy-to-understand as possible, while still being flexible and powerful. This is built on no external dependencies, and works through ctypes in a very obvious fashion.

This works without any other external dependencies.

The code is available on GitHub and the documentation is available on ReadTheDocs. The package itself is available on PyPi.

Installation

You know the drill:

pip install asyncinotify

Usage

The core of this package is asyncinotify.Inotify. Most important Classes may be imported directly from the asyncinotify package.

from pathlib import Path
from asyncinotify import Inotify, Mask
import asyncio

async def main():
    # Context manager to close the inotify handle after use
    with Inotify() as inotify:
        # Adding the watch can also be done outside of the context manager.
        # __enter__ doesn't actually do anything except return self.
        # This returns an asyncinotify.inotify.Watch instance
        inotify.add_watch('/tmp', Mask.ACCESS | Mask.MODIFY | Mask.OPEN | Mask.CREATE | Mask.DELETE | Mask.ATTRIB | Mask.CLOSE | Mask.MOVE | Mask.ONLYDIR)
        # Iterate events forever, yielding them one at a time
        async for event in inotify:
            # Events have a helpful __repr__.  They also have a reference to
            # their Watch instance.
            print(event)

            # the contained path may or may not be valid UTF-8.  See the note
            # below
            print(repr(event.path))

asyncio.run(main())

This will asynchronously watch the /tmp directory and report events it encounters.

This library also supports synchronous operation, using the asyncinotify.inotify.Inotify.sync_get` method, or simply using synchronous iteration.

Support

You can examine our testing matrix to see all the hosts that we actively test on.

Additionally, there is consideration and code for the following platforms, though they are not tested by the maintainer of this project:

We regularly remove out-of-support OSes from the tests and add new releases.

We support back to Python 3.6 as long as it remains easy to do so. We may, in some future version, restrict Python support to non-EOL versions, if supporting the older versions becomes inconvenient.

Motivation

There are a few different python inotify packages. Most of them either have odd conventions, expose too much of the underlying C API in a way that I personally don’t like, are badly documented, they work with paths in a non-idiomatic way, are not asynchronous, or are overengineered compared to the API they are wrapping. I find that the last one is true for the majority of them.

I encourage everybody to read the sources of this package. They are quite simple and easy to understand.

This library

  • Works in a very simple way. It does not have many add-ons or extra features beyond presenting a very Python interface to the raw inotify functionality. Any extra features (such as recursive watching) are presented as completely separate classes. The core inotify functionality is as pure as it can reasonably be.

  • Grabs events in bulk and caches them for minor performance gains.

  • Leverages IntFlag for all masks and flags, allowing the user to use the features of IntFlag, such as seeing individual applied flags in the repr, checking for flag set bits with in.

  • Exposes all paths via python’s pathlib

  • Exposes all the functionality of inotify without depending on the user having to interact with any of the underlying mechanics of Inotify. You should never have to touch the inotify or watch descriptors for any reason.

The primary motivation is that this is written to be a Python inotify module that I would feel comfortable using.

License

This code is Copyright 2019 - 2023 Absolute Performance, Inc, and 2024 - 2026 ProCern Technology Solutions.

It is written and maintained by Taylor C. Richberger.

This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

Note that this does not have the Exhibit B - “Incompatible With Secondary Licenses” Notice. This software is explicitly compatible with secondary licenses. It is MPL 2.0 to ensure that any improvements and other changes are distributed. We do not want to inhibit nor prohibit its use with other FOSS or proprietary software, regardless of the license. In other words, if you aren’t changing asyncinotify, you can pretty much just use it as a library without worrying, unless your other license is explicitly or implicitly incompatible with it (which should be incredibly uncommon).

Project details


Download files

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

Source Distribution

asyncinotify-4.4.4.tar.gz (20.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

asyncinotify-4.4.4-py3-none-any.whl (20.2 kB view details)

Uploaded Python 3

File details

Details for the file asyncinotify-4.4.4.tar.gz.

File metadata

  • Download URL: asyncinotify-4.4.4.tar.gz
  • Upload date:
  • Size: 20.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for asyncinotify-4.4.4.tar.gz
Algorithm Hash digest
SHA256 a8afc92bec6666807ca50524156fca22655325cba6e2b51d842b8ec0d399c708
MD5 6199dc59a9c02704997e0d0421daaafd
BLAKE2b-256 2390f04f0a0c2001e43b3b5199ef6c63c117e43e80cdbaab3037b24c57feeef5

See more details on using hashes here.

File details

Details for the file asyncinotify-4.4.4-py3-none-any.whl.

File metadata

  • Download URL: asyncinotify-4.4.4-py3-none-any.whl
  • Upload date:
  • Size: 20.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.3 {"installer":{"name":"uv","version":"0.11.3","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for asyncinotify-4.4.4-py3-none-any.whl
Algorithm Hash digest
SHA256 9f4236249d78af4451eb689d0c1ed1f52ab5d44a22c7dd699f3dd34f2c3e01f4
MD5 d9ecdd49b751ba3bec815d4130f7de77
BLAKE2b-256 41310ac34c340f517a06cf944c2b4a518c53addf5663df7c704ee711b5e5c662

See more details on using hashes here.

Supported by

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