async-std icon indicating copy to clipboard operation
async-std copied to clipboard

Feature request: block_on_blocking

Open e00E opened this issue 4 years ago • 1 comments

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.

e00E avatar May 29 '20 10:05 e00E

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 dropping local_var and invalidating the reference passed into the unblock() 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.

e00E avatar Jun 02 '20 11:06 e00E