Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run Pyodide in a Web Worker #5

Open
georgestagg opened this issue Jul 24, 2024 · 3 comments
Open

Run Pyodide in a Web Worker #5

georgestagg opened this issue Jul 24, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@georgestagg
Copy link
Member

Comlink should work well here.

Needs research -- Can we distribute a .js worker script with a Quarto reveal.js plugin without executing it?

@georgestagg georgestagg added the enhancement New feature or request label Jul 24, 2024
@georgestagg
Copy link
Member Author

This may be doable without shipping a separate resource file using URL.createObjectURL(), Blob(), and bundling the worker script embedded with esbuild's loader mechanism.

@coatless
Copy link

I was playing around with doing an inline worker:

coatless-quarto/pyodide#12

The most promising approach is to use a Blob web worker similar to what is proposed on SO by vsync:

<!DOCTYPE html>
<script id="worker1" type="javascript/worker">
  // This script won't be parsed by JS engines because its type is javascript/worker.
  self.onmessage = function(e) {
    self.postMessage('msg from worker');
  };
  // Rest of your worker code goes here.
</script>
<script>
  var blob = new Blob([
    document.querySelector('#worker1').textContent
  ], { type: "text/javascript" })

  // Note: window.webkitURL.createObjectURL() in Chrome 10+.
  var worker = new Worker(window.URL.createObjectURL(blob));
  worker.onmessage = function(e) {
    console.log("Received: " + e.data);
  }
  worker.postMessage("hello"); // Start the worker.
</script>

@georgestagg
Copy link
Member Author

georgestagg commented Jul 26, 2024

Indeed, using createObjectURL() and new Blob() in that way works well.

Initial implementation work here: main...pyodide-worker, combined with esbuild text bundling rather than loading from a <script> in the DOM.

Still, it will need more work to get plotting back up and running. Will need to either use tricks like using the bitmap AGG backend and sending results back as base64 encoded .png, or use the custom OffscreenCanvas based renderer developed for quarto-live.

Will return to this after quarto-live initial release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants