first commit.

This commit is contained in:
2023-11-14 00:04:01 +08:00
commit dd23d4184d
126 changed files with 3368 additions and 0 deletions

29
docs/public/worker.js Normal file
View File

@@ -0,0 +1,29 @@
// webworker.js
// Setup your project to serve `py-worker.js`. You should also serve
// `pyodide.js`, and all its associated `.asm.js`, `.json`,
// and `.wasm` files as well:
importScripts("https://cdn.jsdelivr.net/pyodide/v0.24.1/full/pyodide.js")
async function loadPyodideAndPackages() {
self.pyodide = await loadPyodide()
}
const pyodideReadyPromise = loadPyodideAndPackages()
self.onmessage = async (event) => {
// make sure loading is done
await pyodideReadyPromise
// Don't bother yet with this line, suppose our API is built in such a way:
const { id, python, input } = event.data
// Now is the easy part, the one that is similar to working in the main thread:
try {
let result = ""
self.pyodide.setStdin({ stdin: () => input })
self.pyodide.setStdout({ batched: (str) => (result = str) })
await self.pyodide.runPythonAsync(python)
self.postMessage({ result, error: '', id })
} catch (error) {
self.postMessage({ result: '', error: error.message, id })
}
}