A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
improve module reloader performance (#1197)
Akshay Agrawald187c6799bec
diff --git a/marimo/_runtime/reload/module_watcher.py b/marimo/_runtime/reload/module_watcher.pyindex 17fe90084e..7e99f09841 100644--- a/marimo/_runtime/reload/module_watcher.py+++ b/marimo/_runtime/reload/module_watcher.py@@ -49,9 +49,9 @@ def is_submodule(src_name: str, target_name: str) -> bool:def _depends_on(src_module: types.ModuleType,- modules_excluded_from_analysis: list[str],target_modules: set[types.ModuleType],failed_filenames: set[str],+ finder: ModuleFinder,) -> bool:"""Returns whether src_module depends on any of target_filenames"""if not hasattr(src_module, "__file__") or src_module.__file__ is None:@@ -60,7 +60,6 @@ def _depends_on(if src_module.__file__ in failed_filenames:return False- finder = ModuleFinder(excludes=modules_excluded_from_analysis)try:finder.run_script(src_module.__file__)except SyntaxError:@@ -104,26 +103,32 @@ def _is_third_party_module(module: types.ModuleType) -> bool:return "site-packages" in pathlib.Path(filepath).parts+def _get_excluded_modules() -> list[str]:+ return [+ modname+ for modname in sys.modules+ if (m := sys.modules.get(modname)) is not None+ and _is_third_party_module(m)+ ]++def _check_modules(modules: dict[str, types.ModuleType],reloader: ModuleReloader,failed_filenames: set[str],+ finder: ModuleFinder,) -> dict[str, types.ModuleType]:"""Returns the set of modules used by the graph that have been modified"""stale_modules: dict[str, types.ModuleType] = {}modified_modules = reloader.check(modules=sys.modules, reload=False)- modules_excluded_from_analysis = [- modname- for modname in sys.modules- if (m := sys.modules.get(modname)) is not None- and _is_third_party_module(m)- ]+ # TODO(akshayka): could also exclude modules part of the standard library;+ # haven't found a reliable way to do this, however.for modname, module in modules.items():if _depends_on(src_module=module,- modules_excluded_from_analysis=modules_excluded_from_analysis,target_modules=set(m for m in modified_modules if m is not None),failed_filenames=failed_filenames,+ finder=finder,):stale_modules[modname] = module@@ -147,6 +152,7 @@ def watch_modules(reloader = ModuleReloader()# modules that failed to be analyzedfailed_filenames: set[str] = set()+ finder = ModuleFinder(excludes=_get_excluded_modules())while not should_exit.is_set():run_is_processed.wait()time.sleep(1)@@ -163,6 +169,7 @@ def watch_modules(modules=modules,reloader=reloader,failed_filenames=failed_filenames,+ finder=finder,)if stale_modules:with graph.lock:@@ -179,6 +186,8 @@ def watch_modules(if mode == "autorun":run_is_processed.clear()enqueue_run_stale_cells()+ # Update excluded modules in case the module set has changed.+ finder.excludes = _get_excluded_modules()class ModuleWatcher:diff --git a/tests/_runtime/reload/test_module_watcher.py b/tests/_runtime/reload/test_module_watcher.pyindex ce27590ba2..1dfcb75517 100644--- a/tests/_runtime/reload/test_module_watcher.py+++ b/tests/_runtime/reload/test_module_watcher.py@@ -56,7 +56,7 @@ async def test_reload_function()# wait for the watcher to pick up the change- await asyncio.sleep(1.5)+ await asyncio.sleep(2.5)assert k.graph.cells[er_1.cell_id].staleassert k.graph.cells[er_2.cell_id].staleassert not k.graph.cells[er_3.cell_id].stale@@ -110,7 +110,7 @@ async def test_reload_nested_module_function(update_file(nested_module, "func = lambda : 2")# wait for the watcher to pick up the change- await asyncio.sleep(1.5)+ await asyncio.sleep(2.5)assert k.graph.cells[er_1.cell_id].staleassert k.graph.cells[er_2.cell_id].staleassert not k.graph.cells[er_3.cell_id].stale@@ -164,7 +164,7 @@ async def test_reload_nested_module_import_module(update_file(nested_module, "func = lambda : 2")# wait for the watcher to pick up the change- await asyncio.sleep(1.5)+ await asyncio.sleep(2.5)assert k.graph.cells[er_1.cell_id].staleassert k.graph.cells[er_2.cell_id].staleassert not k.graph.cells[er_3.cell_id].stale@@ -269,7 +269,7 @@ async def test_reload_package(update_file(nested_module, "func = lambda : 2")# wait for the watcher to pick up the change- await asyncio.sleep(1.5)+ await asyncio.sleep(2.5)assert k.graph.cells[er_1.cell_id].staleassert k.graph.cells[er_2.cell_id].staleassert not k.graph.cells[er_3.cell_id].stale@@ -324,7 +324,7 @@ async def test_reload_third_party()# wait for the watcher to pick up the change- await asyncio.sleep(1.5)+ await asyncio.sleep(2.5)assert k.graph.cells[er_1.cell_id].staleassert k.graph.cells[er_2.cell_id].staleassert not k.graph.cells[er_3.cell_id].stale