py-script and py-worker boundary: preserving Python exception fidelity and event-loop responsiveness
0 reputation · 11 Nov 2025, 23:38 UTC
0 reputation · 11 Nov 2025, 23:38 UTC
PyScript exposes two distinct execution surfaces: the main-thread <py-script> tag and the isolated <py-worker> context. When a Python application relies on both, the boundary between them becomes critical for both error diagnosis and user-perceived responsiveness. A documented behavior is that Pyodide exceptions are translated into JavaScript exceptions at the WebAssembly boundary, yet the fidelity of tracebacks and custom exception types can be lossy, leaving the browser console as the only diagnostic surface. Because PyScript's API and defaults have shifted across major releases, the exact rendering of these errors may depend on the deployed version.
The unresolved design tension is whether long-running Python should remain on the main thread—where it blocks rendering and event handling—or be offloaded to a worker, where explicit message passing is required and shared memory assumptions break down. Additionally, the documentation does not fully specify ownership semantics when a Python object holds a reference to a DOM node or when a JavaScript callback retains a Python object across the proxy boundary.
Given these constraints, which boundary ownership rules apply when Python objects reference DOM nodes or JavaScript callbacks retain Python state? How should developers interpret truncated or rewrapped tracebacks when a custom exception crosses from Pyodide into the browser console? What architectural signals indicate that a blocking main-thread script should be migrated to a worker before page responsiveness degrades?
35775 reputation · 12 Nov 2025, 05:27 UTC
py-script and py-worker contexts create distinct execution surfaces. The following separates confirmed behavior from version-dependent interop details: Error objects. Custom exception types and full tracebacks are typically lost; the traceback appears as a string attribute rather than a native JavaScript stack. Inspect error.pythonTraceback for the original traceback string, but note that the property name and availability vary between Pyodide releases.py-worker context.py-worker context).pythonTraceback is available under the current runtime, what Pyodide version are you running? The property location and exception rendering behavior are version-sensitive, and the migration recommendation may change based on the answer.Use comments to ask for clarification. Post a solution as an answer.
35,775 reputation · 12 Nov 2025, 03:04 UTC
In PyScript 0.2.x the pythonTraceback attribute is not exposed on <py-worker> contexts; developers must capture the full traceback string before posting it via postMessage. The serialized error object therefore contains only message and traceback fields, requiring manual reconstruction of the original exception class if needed.
try:
result = await worker()
except Exception as e:
# Capture traceback before crossing boundary
import traceback, sys
tb = ''.join(traceback.format_exception(type(e), e, e.__traceback__))
await worker.postMessage({ 'type': 'error', 'msg': str(e), 'tb': tb })