From: Benjamin Braatz Date: Mon, 9 Mar 2026 16:04:45 +0000 (+0100) Subject: Feature #9438: Replace pyinotify with asyncinotify X-Git-Tag: v0.4.0 X-Git-Url: http://git.graph-it.com/?a=commitdiff_plain;p=graphit%2Fcontrolpi.git Feature #9438: Replace pyinotify with asyncinotify --- diff --git a/controlpi/__init__.py b/controlpi/__init__.py index 970e130..155becc 100644 --- a/controlpi/__init__.py +++ b/controlpi/__init__.py @@ -90,10 +90,7 @@ async def run(conf: Dict[str, PluginConf]) -> None: """ message_bus = MessageBus() coroutines = _process_conf(message_bus, conf) - try: - await asyncio.gather(*coroutines) - except asyncio.CancelledError: - pass + await asyncio.gather(*coroutines) async def test( diff --git a/controlpi/__main__.py b/controlpi/__main__.py index 7f0b7f5..ef02d61 100644 --- a/controlpi/__main__.py +++ b/controlpi/__main__.py @@ -7,12 +7,11 @@ started by: import signal import sys -import os import json import asyncio -import pyinotify +import asyncinotify -from controlpi import run, PluginConf +import controlpi from typing import Dict @@ -22,6 +21,8 @@ async def shutdown(sig: signal.Signals) -> None: print(f"Shutting down on signal {sig.name}.") for task in asyncio.all_tasks(): task.cancel() + global restart + restart = False async def add_signal_handlers() -> None: @@ -31,7 +32,7 @@ async def add_signal_handlers() -> None: loop.add_signal_handler(sig, lambda s=sig: asyncio.create_task(shutdown(s))) -def read_configuration() -> Dict[str, PluginConf]: +def read_configuration() -> Dict[str, controlpi.PluginConf]: """Read configuration from JSON file.""" conf = {} try: @@ -46,34 +47,28 @@ def read_configuration() -> Dict[str, PluginConf]: return conf -class ConfigHandler(pyinotify.ProcessEvent): - """Handler for changes of the configuration file on disk.""" - - def process_IN_MODIFY(self, event): - """Cancel all tasks if configuration file changed.""" - if event.pathname == os.path.abspath(sys.argv[1]): - print(f"Configuration file modified: {event.pathname}") +async def watch_config() -> None: + """Watch the configuration file for modifications.""" + with asyncinotify.Inotify() as inotify: + inotify.add_watch(sys.argv[1], asyncinotify.Mask.MODIFY) + async for event in inotify: + print("Configuration file modified.") for task in asyncio.all_tasks(): task.cancel() -async def add_config_change_handler() -> pyinotify.AsyncioNotifier: - """Add handler for configuration file.""" - wm = pyinotify.WatchManager() - loop = asyncio.get_running_loop() - notifier = pyinotify.AsyncioNotifier(wm, loop, default_proc_fun=ConfigHandler()) - wm.add_watch(os.path.dirname(sys.argv[1]), pyinotify.ALL_EVENTS) - return notifier - - async def main() -> None: """Set up signal handlers, read configuration file and run system.""" await add_signal_handlers() - notifier = await add_config_change_handler() conf = read_configuration() - await run(conf) - notifier.stop() + await asyncio.gather(controlpi.run(conf), watch_config()) if __name__ == "__main__": - asyncio.run(main()) + global restart + restart = True + while restart: + try: + asyncio.run(main()) + except asyncio.exceptions.CancelledError: + pass diff --git a/setup.py b/setup.py index 47bed5d..abd0834 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ with open("README.md", "r") as readme_file: setuptools.setup( name="controlpi", - version="0.3.0", + version="0.4.0", author="Graph-IT GmbH", author_email="info@graph-it.com", description="Control-Pi Infrastructure", @@ -17,7 +17,7 @@ setuptools.setup( zip_safe=False, install_requires=[ "fastjsonschema", - "pyinotify", + "asyncinotify", ], classifiers=[ "Programming Language :: Python",