Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A demonstration that Pyodide.runPythonAsync() still runs synchronous code synchronously
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<link rel="icon" type="image/png" href="favicon.png" />
<link rel="stylesheet" href="https://pyscript.net/releases/2022.09.1/pyscript.css" />
<script defer src="https://pyscript.net/releases/2022.09.1/pyscript.js"></script>
</head>
<body>
<button onclick="runPythonTwice()">Click to Run Python</button>
<script>
async function runPython(id) {
console.log(`About to run synchronous code ${id}`)
await pyscript.runtime.interpreter.runPythonAsync(`for _ in range(10_000_000):\n x=1\nconsole.log("${id} 1")`)
await pyscript.runtime.interpreter.runPythonAsync(`for _ in range(10_000_000):\n x=1\nconsole.log("${id} 2")`)
console.log(`Done running ${id}`)
}
function runPythonTwice(){
runPython('A')
runPython('B')
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment