Does PyScript queue concurrent <py-script> executions on a single shared interpreter?
0 reputation · 14 Feb 2024, 11:06 UTC
0 reputation · 14 Feb 2024, 11:06 UTC
37775 reputation · 14 Feb 2024, 17:28 UTC
No. PyScript does not queue concurrent <py-script> executions; it runs all Python code on a single shared interpreter that lives on the browser’s main JavaScript event loop.
The Pyodide runtime embeds CPython inside a single WebAssembly module. When a <py-script> block loads, its code is compiled and executed on that same interpreter. Because the interpreter occupies the main thread, any long‑running Python call blocks the event loop, preventing other JavaScript handlers or later <py-script> blocks from running until the call returns.
<py-script> tags on the same page share the same global interpreter state; variables, imports and classes defined in one block are visible to subsequent blocks.window.my_val = 42) in the first <py-script> block.<py-script> block, read that variable and log its value. If the value is 42, the interpreter is shared.for i in range(10_000_000): pass) in one block and click a button that triggers a simple alert in another block. If the alert does not appear until the loop finishes, the interpreter is not queued.Use comments to ask for clarification. Post a solution as an answer.
37,775 reputation · 14 Feb 2024, 20:03 UTC
A shared interpreter also means shared Python global state: modules imported and variables assigned in one <py-script> block are visible to later blocks on the same page unless they are explicitly scoped. Because Pyodide runs on the browser main thread by default, a long-running synchronous block serializes everything else—clicks, timers, and other <py-script> bodies wait on the JavaScript event loop even if they are marked async; await only yields at suspension points, it does not start a new thread.
To verify against your deployed version, open DevTools, run a several-second synchronous Python loop in one tag, then trigger another tag or click a button. If the page freezes and the second invocation does not start until the first completes, you are seeing main-thread serialization on one interpreter. Treat any claim about a specific PyScript version introducing per-tag or per-worker isolation as needing changelog confirmation, since that API surface is not stable.