async-std
async-std copied to clipboard
Feature request: block_on_blocking
async_std::task::spawn
spawns an asynchronous task.
async_std::task::spawn_blocking
spawns a blocking task.
Both require the future/function to be 'static
.
async_std::task::block_on
is like spawn
but does not require 'static
.
In my mind this makes sense because we ensure that the future is used immediately. It works like crossbeam's scope threads.
If block_on
is like spawn
without 'static
then there should be an equivalent for spawn_blocking
without 'static
. It could be called block_on_blocking
.
https://github.com/stjepang/blocking/issues/4#issuecomment-633935605 explains that this might not be possible:
Unfortunately, that is not possible and we can't use scoped threads. To see why, consider:
async { let local_var = "foo".to_string(); unblock(|| something(&local_var)).await; }
A solution with scoped threads would have to block the main threads until spawned threads complete. But we're not allowed to block inside futures.
Furthermore, we have to assume that the main future (the big
async {}
block here) can be dropped at any time, thus droppinglocal_var
and invalidating the reference passed into theunblock()
closure.There are probably ways to solve this problem in one way way or another, but I haven't found a satisfying solution without deal-breaking tradeoffs yet.