Skip to content

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.

perf: skip variable broadcast in run mode (#3636)

Myles Scolnick73ba1650696b
diff --git a/marimo/_runtime/runner/hooks_post_execution.py b/marimo/_runtime/runner/hooks_post_execution.py
index aef7a862aa..d374376ba2 100644
--- a/marimo/_runtime/runner/hooks_post_execution.py
+++ b/marimo/_runtime/runner/hooks_post_execution.py
@@ -26,9 +26,11 @@ from marimo._messaging.ops import (
from marimo._messaging.tracebacks import write_traceback
from marimo._output import formatting
from marimo._plugins.ui._core.ui_element import UIElement
+from marimo._runtime.context.kernel_context import KernelRuntimeContext
from marimo._runtime.context.types import get_context, get_global_context
from marimo._runtime.control_flow import MarimoInterrupt, MarimoStopError
from marimo._runtime.runner import cell_runner
+from marimo._server.model import SessionMode
from marimo._tracer import kernel_tracer
from marimo._utils.flatten import contains_instance
@@ -88,6 +90,10 @@ def _broadcast_variables(
runner: cell_runner.Runner,
run_result: cell_runner.RunResult,
) -> None:
+ # Skip if not in edit mode
+ if not _is_edit_mode():
+ return
+
del run_result
values = [
VariableValue(
@@ -108,6 +114,10 @@ def _broadcast_datasets(
runner: cell_runner.Runner,
run_result: cell_runner.RunResult,
) -> None:
+ # Skip if not in edit mode
+ if not _is_edit_mode():
+ return
+
del run_result
tables = get_datasets_from_variables(
[
@@ -127,6 +137,10 @@ def _broadcast_duckdb_tables(
runner: cell_runner.Runner,
run_result: cell_runner.RunResult,
) -> None:
+ # Skip if not in edit mode
+ if not _is_edit_mode():
+ return
+
del run_result
del runner
if not DependencyManager.duckdb.has():
@@ -301,6 +315,14 @@ def _reset_matplotlib_context(
exec("__marimo__._output.mpl.close_figures()", runner.glbls)
+def _is_edit_mode() -> bool:
+ ctx = get_context()
+ return (
+ isinstance(ctx, KernelRuntimeContext)
+ and ctx.session_mode == SessionMode.EDIT
+ )
+
+
POST_EXECUTION_HOOKS: list[PostExecutionHookType] = [
_set_imported_defs,
_set_run_result_status,
diff --git a/marimo/_runtime/runtime.py b/marimo/_runtime/runtime.py
index af60073ccc..94f0e82547 100644
--- a/marimo/_runtime/runtime.py
+++ b/marimo/_runtime/runtime.py
@@ -419,6 +419,7 @@ class Kernel:
def __init__(
self,
+ *,
cell_configs: dict[CellId_t, CellConfig],
app_metadata: AppMetadata,
user_config: MarimoConfig,
diff --git a/tests/_utils/test_file_watcher.py b/tests/_utils/test_file_watcher.py
index 6522caf4aa..88d0e6b125 100644
--- a/tests/_utils/test_file_watcher.py
+++ b/tests/_utils/test_file_watcher.py
@@ -3,10 +3,13 @@ from __future__ import annotations
import asyncio
import os
+import sys
from pathlib import Path
from tempfile import NamedTemporaryFile
from typing import List
+import pytest
+
from marimo._utils.file_watcher import FileWatcherManager, PollingFileWatcher
@@ -43,6 +46,10 @@ async def test_polling_file_watcher() -> None:
assert callback_calls[0] == tmp_path
+ sys.version_info < (3, 10),
+ reason="File watcher tests require Python 3.10+",
+)
async def test_file_watcher_manager() -> None:
# Create two temporary files
with (