-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Running *part* of your wasm in a web_sys::Worker
?
#2549
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
Comments
Background work is unfortunately somewhat difficult, even today. This is largely due to the ecosystem being relatively immature around multithreading and how the multithreading story in a browser is so different from a native version. I'd recommend following the parallel raytrace example if you can, but that's all the advice that I would have. For other advice I'd try reaching out to others in other forums perhaps. |
Alright, thank you any way @alexcrichton 🙂 I will take a closer look at the raytrace example then. |
I could not let it go and finally figured it out! 😃 One of the main blockers for me was that Firefox does not support importing ES modules in workers - with my limited JS knowledge, it took me a while to nail down the problem as this. To be able to go back to it, I created four example starting from a very basic example with modules (which only works in Chrome) over using |
More examples always seems good to me! |
Great, I opened a pull request :) Since it is my first contribution to |
This commit adds a parallel execution example in which we spawn a web worker with `web_sys`, run WASM code in the web worker and interact between the main thread and the web worker. It is intended to add an easy starting point for parallel execution with WASM, complementing the more involved parallel raytrace example. Related to GitHub issue #2549 Co-authored-by: Simon Gasse <[email protected]>
Summary
What is the best architectural approach to have a 'pure' wasm web application that runs part of the calculations in a web worker?
Additional Details
I am still fairly new to Rust and especially web_sys. However I find the concept of wasm really exciting and I am currently trying out to build an easy web app. It needs to react to clicks on a canvas, some buttons etc., but then there are heavier calculations which should run in the background.
Initially, I followed the Wasm Game of Life tutorial and found a good first entry point there. After reading this guide, I designed my app to handle the user interaction part in JS. The background calculation was running actually in the foreground but returned very regularly, giving a false impression of responsiveness.
Next I read about web_sys and especially building wasm apps that can be deployed without a bundler. Now I rewrote the user interaction part almost purely in Rust and web_sys. Knowing that there is probably no native multiprocessing in wasm as of now, web workers are the only alternative.
My dream scenario would be to spawn a
web_sys::Worker
and hand it a struct with a method or a function callback to run. However it looks like a web worker expects a URL to execute (1, 2). Loading in any local JS via the URL works, but not a wasm module.If I create a worker in my main wasm app
and have this minimal
worker.js
:I get the error
SyntaxError: import declarations may only appear at top level of a module
. This might have JS reasons (not so familiar with JS ^^). Though maybe someone could point me in the right direction? Which one of the following options is possible? And which one is best?init
. I think it is not possible to have two wasm targets in the samecargo.toml
so it would be annoying to maintain two project folders for essentially one projects. Also I would need to figure out a better way to load it given that the above approach at least for some imported module was not allowed in the worker.Worker::set_onmessage
etc. - this seems best to me but I am not sure if it is possible at all.web_sys
. I don't like this option very much because it would mean having to dig deeper into JS for me.Are there any good tutorials on this? I know about the Parallel Raytracing example but I could not really extract how to use the
web_sys::Worker
from it. Also this WASM Fractal Generator seems to have the worker and canvas interaction in JS, so it is not really what I am looking for.Any hint/explanation would be greatly appreciated!
The text was updated successfully, but these errors were encountered: