4 ms·
I think the JS world might be particularly excited about async/await because other people just escape to threads when they don't want to block the whole thing o
by ben0x539 8y ago
I think the JS world might be particularly excited about async/await because other people just escape to threads when they don't want to block the whole thing on IO.
Both in JS or Rust, you don't gain anything just by declaring your thing to be async, or awaitable. Your function needs to be built around some kind of "primitive" that explicitly supports the "do something else in the meantime" mechanism. Using "await" on that thing lets your function piggyback on its support, but all your explicit, normal code is synchronously blocking as usual.
In Rust, I think it's a fairly established pattern to turn blocking code, where the blocking part is not some IO action that has explicit support for the futures mechanism, into a asynchronous, awaitable function by punting the work to a threadpool. That makes sense for CPU-bound work as well as IO done by libraries that don't support futures or things like disk IO where the OS might not actually have decent support for doing it in a non-blocking fashion.
I'm not sure if it's the canonical mechanism, but this crate seems to implement what I'm thinking of: https://docs.rs/futures-cpupool/0.1.8/futures_cpupool/ https://docs.rs/futures-cpupool/0.1.8/futures_cpupool/
- steveklabnik 8y agoThe latest tokio has a work-stealing threadpools, so you don’t need to explicitly do this anymore, IIRC.
- oconnor663 8y agoNeat! Do you know if there's anything in there resembling the rayon::join API? I didn't see anything on a cursory look.
- steveklabnik 8y agoI am not sure, to be honest. I've been waiting until everything settles down to really dig in.
- ZeikJT 8y ago> Both in JS or Rust, you don't gain anything just by declaring your thing to be async, or awaitable. I'm not familiar with Rust but in JS you do gain something. Just the fact that the function is async means that it now explicitly returns a promise, which means that anything awaiting that promise will be in a new execution context and will definitely not run synchronously.