This repository was archived by the owner on May 20, 2024. It is now read-only.
Replies: 2 comments 2 replies
-
|
I've achieved this goal without any modification to the main process (I'm not sure if this concept is efficient but it does the job by terminating the current process and start a new process): import subprocess
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class RestartOnChangeHandler(FileSystemEventHandler):
def __init__(self, command):
self.command = command
self.pid: subprocess.Popen[bytes]
def start(self):
self.pid = subprocess.Popen(self.command)
def on_any_event(self, event):
print(f"Change detected: {event.src_path}. Restarting script...")
self.pid.terminate()
self.pid = subprocess.Popen(self.command)
def main(script_path):
path = './configs'
command = ['python', script_path]
event_handler = RestartOnChangeHandler(command=command)
observer = Observer()
observer.schedule(event_handler, path, recursive=True)
observer.start()
print(f"Monitoring {path} for changes. Running {script_path}...")
event_handler.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
if __name__ == "__main__":
main('autofishbot.py') |
Beta Was this translation helpful? Give feedback.
2 replies
-
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment

Uh oh!
There was an error while loading. Please reload this page.
-
Hello, I would like to suggest a feature that is about reloading the config details upon any change to the running config file and resuming the process with the new config. I've tried to do it but had no enough knowledge about how to do it due to the complexity of how the program is coded.
Beta Was this translation helpful? Give feedback.
All reactions