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

Blob inline web worker #12

Open
coatless opened this issue Mar 2, 2024 · 0 comments
Open

Blob inline web worker #12

coatless opened this issue Mar 2, 2024 · 0 comments
Labels
p: high Address issue at next possible opening t: feature-request

Comments

@coatless
Copy link
Collaborator

coatless commented Mar 2, 2024

As we start adding onto the initialization routine, we should move away from placing Pyodide on the main thread and onto a separate worker thread. This will give less of a "freeze" feeling while still allowing us to maintain a single shared environment. Plus, this allows us to move away from the matplotlib-pyodide's backends in favor just an IO stream re-capture.

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>

By the looks of it, the use of a blob will be supported on modern browsers (~ < 5 years)

https://caniuse.com/bloburls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p: high Address issue at next possible opening t: feature-request
Projects
None yet
Development

No branches or pull requests

1 participant