reactpy icon indicating copy to clipboard operation
reactpy copied to clipboard

Use WatchGod instead of Custom File Watching Solution

Open rmorshea opened this issue 3 years ago • 3 comments
trafficstars

Current Situation

Currently we use a custom built file watching solution when running examples locally. However there are already great tools for doing this.

Proposed Actions

We can just swap out our solution for watchgod.

rmorshea avatar Feb 18 '22 22:02 rmorshea

I would just like to point out, if idom.run() is supposed to automatically refresh on file changes, then that is not working under Windows 10.

Archmonger avatar Feb 20 '22 08:02 Archmonger

It doesn't because Sanic isn't run in debug mode by default. This is mostly about a dev tool I have for running examples from the docs.

rmorshea avatar Feb 20 '22 20:02 rmorshea

I didn't know this was supposed to work, I built my own watching script for developing in idom:

import time
from watchdog.observers import Observer
from watchdog.events import PatternMatchingEventHandler
import subprocess


class ScanPyFiles:
    def __init__(self, process: subprocess, cmd: str):
        self.process = process
        self.event_handler = PatternMatchingEventHandler(
            patterns=["*.py"], ignore_patterns=[], ignore_directories=True
        )
        self.event_handler.on_any_event = self.on_any_event
        self.observer = Observer()
        self.observer.schedule(self.event_handler, ".", recursive=True)
        self.observer.start()

    def on_any_event(self, event):
        subprocess.Popen("TASKKILL /F /PID {pid} /T".format(pid=self.process.pid))
        self.process = subprocess.Popen(cmd, shell=True)
        print("Server Restarted")

    def stop(self):
        self.observer.stop()
        self.observer.join()


cmd = "python3 run_app.py"
process = subprocess.Popen(cmd, shell=True)
scan_py_files = ScanPyFiles(process, cmd)

try:
    while True:
        time.sleep(1)
except KeyboardInterrupt:
    print("Ouch !!! Keyboard interrupt received.")

scan_py_files.stop()

acivitillo avatar Mar 03 '22 08:03 acivitillo