tokio-rs / tokio

A runtime for writing reliable asynchronous applications with Rust. Provides I/O, networking, scheduling, timers, ...
https://tokio.rs
MIT License
26.29k stars 2.42k forks source link

Full-featured async support in the web (WASM)? #6178

Open fzyzcjy opened 9 months ago

fzyzcjy commented 9 months ago

Is your feature request related to a problem? Please describe.

Hi, thanks for the helpful async runtime! When working on https://github.com/fzyzcjy/flutter_rust_bridge v2, more specifically the async Rust support, I realize that tokio's async does not have full features on the web.

https://docs.rs/tokio/latest/tokio/#wasm-support says that, rt is supported, but rt-multi-thread is not. However, it seems useful sometimes to leverage multi threads instead of only one thread.

There seems already exists ways to utilize multiple threads in WASM. For example, here is a "thread pool" example https://github.com/rustwasm/wasm-bindgen/blob/main/examples/raytrace-parallel/src/pool.rs. In flutter_rust_bridge, we also already utilize that code (with citations, surely) for a slightly more general purposed worker pool: https://github.com/fzyzcjy/flutter_rust_bridge/blob/6f3fa295d192a3945aab6c63ab6b533dc2aef2a9/frb_rust/src/wasm_bindgen_src/pool.rs. Memory seems also possible to be shared between workers via SharedArrayBuffer.

Therefore, I wonder whether tokio's async system will be full-featured in WASM in the future? Thanks!

Describe the solution you'd like (see above)

Describe alternatives you've considered (see above)

Additional context (see above)

Darksonn commented 9 months ago

I don't really know that much about wasm. Maybe in the future, but not right now.

It's also important to distinguish between web wasm and non-web wasm. It makes a difference whether we can block for extended durations of time.

fzyzcjy commented 9 months ago

Thank you!

It's also important to distinguish between web wasm and non-web wasm. It makes a difference whether we can block for extended durations of time.

I am not sure, but it seems that the web workers (which tokio may create) do not block the main thread.

Darksonn commented 9 months ago

Right. Ultimately, that just means there are three cases to distinguish between, not two. Wasi and web worker are not really the same in terms of what operations they expose as far as I understand.

Darksonn commented 7 months ago

Closing as duplicate of #4827.

banocean commented 7 months ago

Tokio supports only what it can support without OS APIs that are exposed by, for example, WASI. WEB API doesn't cover anything close to that, so I'd consider it impossible.

Darksonn commented 7 months ago

You're right, I've reopened this.