Skip to content

Commit

Permalink
improve ipyflow integration and handle case where ipyflow already ins…
Browse files Browse the repository at this point in the history
…tantiated reloader singleton
  • Loading branch information
smacke committed Nov 7, 2023
1 parent 5a5b387 commit 52d057a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
7 changes: 6 additions & 1 deletion core/superduperreload/magics.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,12 @@ def __init__(self, *a, **kw):
raise RuntimeError("CPython required for superduperreload extension")
flow = kw.pop("flow", None)
super().__init__(*a, **kw)
self._reloader = ModuleReloader.instance(self.shell, flow=flow)
if ModuleReloader.initialized():
self._reloader = ModuleReloader.instance()
self._reloader.enabled = True
self._reloader.start_watcher_thread_if_applicable()
else:
self._reloader = ModuleReloader.instance(self.shell, flow=flow, enabled=True)
self.loaded_modules = set(sys.modules)

@line_magic
Expand Down
19 changes: 11 additions & 8 deletions core/superduperreload/superduperreload.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,11 @@ def __init__(
self,
shell: Optional[Union["InteractiveShell", "FakeShell"]] = None,
flow: Optional["NotebookFlow"] = None,
enabled: bool = True,
) -> None:
super().__init__(patch_referrers=SHOULD_PATCH_REFERRERS)
# Whether this reloader is enabled
self.enabled = True
self.enabled = enabled
# Whether to print reloaded modules and other messages
self.verbose = True
# Modules that failed to reload: {module: mtime-on-failed-reload, ...}
Expand Down Expand Up @@ -142,10 +143,14 @@ def __init__(
self.check(do_reload=False)
self._watcher: Optional[Thread] = None
self._watcher_running = False
if self.flow is not None:
self._watcher = Thread(target=self._watch, daemon=True)
self._watcher_running = True
self._watcher.start()
self.start_watcher_thread_if_applicable()

def start_watcher_thread_if_applicable(self) -> None:
if self._watcher is not None or self.flow is None or not self.enabled:
return
self._watcher = Thread(target=self._watch, daemon=True)
self._watcher_running = True
self._watcher.start()

@classmethod
def clear_instance(cls) -> None:
Expand Down Expand Up @@ -313,9 +318,7 @@ def handle_module_refreshed(
pass

def _watch(self, interval: float = 1) -> None:
assert self.flow is not None
for m in list(sys.modules.values()):
self.handle_module_refreshed(m)
assert self.enabled and self.flow is not None
while self._watcher_running:
try:
with self._reloading_lock:
Expand Down

0 comments on commit 52d057a

Please sign in to comment.