Skip to content

watcher

REFRESH_SECONDS module-attribute

Python
REFRESH_SECONDS = 2.0

How often the directories being watched are compared with the modules that are imported now.

DEBOUNCE_MS module-attribute

Python
DEBOUNCE_MS = 1600

How long watchfiles waits for a burst of saves to settle before reporting them as one batch.

FileWatcher dataclass

Python
FileWatcher(
    path: Path,
    on_change: Callable[[int], None],
    on_stopped: Callable[[str], None] | None = None,
)

Watches the code a reload would pick up for *.py changes on a background thread and reports how many files changed in each batch.

path instance-attribute

Python
path: Path

The project directory. It and the directories of the project's imported modules are watched, each on its own.

on_change instance-attribute

Python
on_change: Callable[[int], None]

Called from the watcher's thread with the number of files that changed.

on_stopped class-attribute instance-attribute

Python
on_stopped: Callable[[str], None] | None = None

Called from the watcher's thread with the reason, if watching ends because of a failure. It is not called when stop ends it.

start

Python
start() -> None

Starts watching. Does nothing if already running.

Source code in src/mujoco_mojo/utils/layers/reloaded/watcher.py
Python
def start(self) -> None:
    """Starts watching. Does nothing if already running."""
    if self.running:
        return
    # a fresh event per run, so a stale thread can never be revived by a later start
    self._stop = threading.Event()
    self._thread = threading.Thread(
        target=self._run,
        args=(self._stop,),
        daemon=True,
        name="reloaded-file-watcher",
    )
    self._thread.start()

stop

Python
stop(timeout: float = 2.0) -> None

Stops watching and waits up to timeout seconds for the thread to finish.

Source code in src/mujoco_mojo/utils/layers/reloaded/watcher.py
Python
def stop(self, timeout: float = 2.0) -> None:
    """Stops watching and waits up to `timeout` seconds for the thread to finish."""
    self._stop.set()
    if self._thread is not None:
        self._thread.join(timeout=timeout)
        self._thread = None

watched_directories

Python
watched_directories(root: Path) -> list[Path]

The directories to watch for a reload under root: root itself and the directory of every imported module that lives under it.

That is the same set of modules a reload acts on, and watching only their directories keeps the number of watches small. Watching everything under root also watches build trees, virtual environments, and checkouts that happen to sit beneath it, which can use up the operating system's whole allowance of watches.

Source code in src/mujoco_mojo/utils/layers/reloaded/watcher.py
Python
def watched_directories(root: Path) -> list[Path]:
    """
    The directories to watch for a reload under `root`: `root` itself and the directory of every imported module that lives under it.

    That is the same set of modules a reload acts on, and watching only their directories keeps the number of watches small. Watching everything under `root` also watches build trees, virtual environments, and checkouts that happen to sit beneath it, which can use up the operating system's whole allowance of watches.
    """
    root = root.resolve()
    found = {root}
    for module in list(sys.modules.values()):
        file = getattr(module, "__file__", None)
        if not file:
            continue
        try:
            directory = Path(file).resolve().parent
        except (OSError, RuntimeError):
            continue
        if (
            directory.is_relative_to(root)
            and not any(part in _INSTALLED_PARTS for part in directory.parts)
            and directory.is_dir()
        ):
            found.add(directory)
    return sorted(found)