pyodide/worker/worker-input.js - Worker Input Handling
Location: src/pyodide/worker/worker-input.js
This file contains the logic for handling Python's input() function within the Pyodide web worker.
Core Functions
setupInputHandling(pyodide)
- Description: This function sets up the necessary infrastructure for handling
input(). It creates arequestInputfunction in the worker's global scope and then runs a Python script to replace the built-ininputwith an async version that callsrequestInput. requestInput(prompt): When the Pythoninput()is called, it triggersrequestInput, which sends an'input_required'message to the main thread and then waits for a promise to be resolved.- Parameters:
pyodide(PyodideAPI): The Pyodide instance.
handleInputResponse(data, workerState)
- Description: This function is called when the worker receives a message of type
'input_response'from the main thread. It takes the input value from the message and uses it to resolve the pending promise created byrequestInput, which unpauses the Python execution. - Parameters:
data(Object): The message data, containing theinputstring.workerState(Object): The current state of the worker.