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.

performance: pyodide code completion (#1084)

Akshay Agrawal1492b9e40847
diff --git a/frontend/src/core/pyodide/bridge.ts b/frontend/src/core/pyodide/bridge.ts
index 73591d32fe..9d22ccaeef 100644
--- a/frontend/src/core/pyodide/bridge.ts
+++ b/frontend/src/core/pyodide/bridge.ts
@@ -35,6 +35,7 @@ import { PyodideRouter } from "./router";
import { getMarimoVersion } from "../dom/marimo-tag";
import { getWorkerRPC } from "./rpc";
import { API } from "../network/api";
+import { RuntimeState } from "@/core/kernel/RuntimeState";
export class PyodideBridge implements RunRequests, EditRequests {
static INSTANCE = new PyodideBridge();
@@ -203,10 +204,15 @@ export class PyodideBridge implements RunRequests, EditRequests {
sendCodeCompletionRequest = async (
request: CodeCompletionRequest,
): Promise<null> => {
- await this.rpc.proxy.request.bridge({
- functionName: "code_complete",
- payload: request,
- });
+ // Because the Pyodide worker is single-threaded, sending
+ // code completion requests while the kernel is running is useless
+ // and runs the risk of choking the kernel
+ if (!RuntimeState.INSTANCE.running()) {
+ await this.rpc.proxy.request.bridge({
+ functionName: "code_complete",
+ payload: request,
+ });
+ }
return null;
};
diff --git a/marimo/_pyodide/pyodide_session.py b/marimo/_pyodide/pyodide_session.py
index 68b886bab3..d2e1e523f9 100644
--- a/marimo/_pyodide/pyodide_session.py
+++ b/marimo/_pyodide/pyodide_session.py
@@ -369,6 +369,9 @@ def launch_pyodide_kernel(
async def listen_completion() -> None:
while True:
request = await completion_queue.get()
+ while not completion_queue.empty():
+ # discard stale requests to avoid choking the runtime
+ request = await completion_queue.get()
LOGGER.debug("received completion request %s", request)
# 5 is arbitrary, but is a good limit:
# too high will cause long load times