From 3b37c8bb1d1067809e2ba65bb109e7210a68fbd1 Mon Sep 17 00:00:00 2001 From: Azalea Gui Date: Tue, 7 Mar 2023 23:08:59 -0500 Subject: [PATCH] [+] Dynamically reload with file watcher --- tngame/__main__.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 tngame/__main__.py diff --git a/tngame/__main__.py b/tngame/__main__.py new file mode 100644 index 0000000..39c00ac --- /dev/null +++ b/tngame/__main__.py @@ -0,0 +1,42 @@ +from multiprocessing import Process +from pathlib import Path + +from watchdog.events import FileSystemEventHandler +from watchdog.observers import Observer + +if __name__ == '__main__': + # Run the game server on a new process + from . import telnet + p = Process(target=telnet.run) + p.start() + + # Listen when any python file in this directory is changed + class MyHandler(FileSystemEventHandler): + def on_modified(self, event): + if not event.src_path.endswith(".py"): + return + + global p + print(f"File changed: {event.src_path}, reloading") + + # Stop the server + p.terminate() + + # Re-import the module + import importlib + importlib.reload(telnet) + + # Run the server again + p = Process(target=telnet.run) + p.start() + + # Get script path + script_path = Path(__file__).parent + + event_handler = MyHandler() + observer = Observer() + observer.schedule(event_handler, path=str(script_path.absolute()), recursive=False) + observer.start() + + print("Watching for changes...") + observer.join()