5 ms·
I started experimenting with Go-WASM recently and learnt that writing non-blocking code is very crucial, Timer even inside a Goroutine blocks & crashes WASM. Le
by Abishek_Muthian 5y ago
I started experimenting with Go-WASM recently and learnt that writing non-blocking code is very crucial, Timer even inside a Goroutine blocks & crashes WASM. Leading me to use unreliable JS timers(albeit mitigating it with a worker) and performing only non-blocking compute on WASM.
Is this just a consequence of using Go for WASM(i.e. lack of WASM threads), What are people who built LibreOffice in WASM or Doom3 using? Can Go-level concurrency be achieved with WASM threads when programmed with other languages?
- khan-saib 5y ago
- tmpfs 5y agoThis doesn't sound right to me, are you running your WASM in a worker so it doesn't block the main UI thread?
- Abishek_Muthian 5y agoI require a Go UI library(ebiten) so wasm cannot be initialized and run purely via a worker, But I've tried executing the timer inside a Goroutine with a handler as recommended by Go team but no dice[1]. There are others who have complained the same on SO. [1] https://github.com/golang/go/issues/41310 https://github.com/golang/go/issues/41310
- miohtama 5y agoWebAssembly is having/going to have threads https://github.com/WebAssembly/threads https://github.com/WebAssembly/threads
- Abishek_Muthian 5y agoGo hasn't enabled it since it's still in proposal stages[1], Hence my question was regarding it's implementation using other languages and general experience of it. [1] https://github.com/golang/go/issues/28631 https://github.com/golang/go/issues/28631
- azakai 5y agoYou may want to file a bug on Go - I believe goroutines should work, including swapping between them. The code is emitted in a form that allows that (switch-in-a-loop pattern). Another thing you can try is TinyGo, whch uses Asyncify (a general wasm transformation to allow blocking) for goroutines, https://twitter.com/TinyGolang/status/1461364896197578759 https://twitter.com/TinyGolang/status/1461364896197578759 Doom3 uses Asyncify in some parts of the event loop, for similar reasons (blocking code). I'm not sure if LibreOffice does as well, but it might.
- Abishek_Muthian 5y agoYou're right, I think I should file a bug. The Go library I need(ebiten) isn't compatible with TinyGo, I did try it for the reason you mentioned and to reduce size. > Doom3 uses Asyncify in some parts of the event loop, for similar reasons (blocking code). I'm not sure if LibreOffice does as well, but it might. Thanks! That's what I wanted to know, So it's certainly possible to write asynchronous code using C++ and Rust.
- azakai 5y agoYes, async code in wasm is definitely possible. See for example here: https://web.dev/asyncify/ https://web.dev/asyncify/ It does have some overhead atm, which a new spec (stack switching) will remove eventually.