I'm using the following code to detect changes in my config, which then restarts the app by replacing the process with a new instance of itself:
import os
import sys
import watchdog.events
import watchdog.observers
class _ConfigUpdateHandler(watchdog.events.FileSystemEventHandler):
def on_modified(self, event: watchdog.events.FileSystemEvent) -> None:
if event.src_path.endswith("config.json"):
os.execl(sys.executable, sys.executable, *sys.argv)
def run_update_listener():
observer = watchdog.observers.Observer()
observer.schedule(_ConfigUpdateHandler(), ".")
observer.start()
This works fine. If I do python3.11 main.py and then save the config.json file, it does restart the app. But here comes the weird part: If I ctrl+c my app, and then do python3.11 main.py again, it spams the on_modified event, ~once per second. I can't even cancel that with ctrl+c, it's stuck in a boot loop. I have no idea why, is that a bug with watchdog? Is it because I apruptly replace the process?