The new runtime detects blocking automatically. We don’t need spawn_blocking anymore and can simply deprecate it. The new runtime makes blocking efficient. Rather than always paying the cost of spawn_blocking, we only offload blocking work to a separate thread if the work really blocks.
They provide unstable spawn_blocking feature. Do I need to use spawn for both blocking and non-blocking tasks, or is their runtime efficient enough to detect blocking task and put in a separate threadpool.
Note : This blog post describes a proposed scheduler for async-std that did not end up being merged for several reasons. We still believe that the concept holds important insight and may circle back to it in the future, so we keep it online as it.
I suspect that they still aren’t using this new scheduler based on the note
If you know something is going to block, you're always better off explicitly spawning it as blocking, instead of relying on the runtime to observe the blocking and try to fix it after the fact.
I don't think that is necessarily true. My mental model ( which may be wrong ) is there is at any time a set of async tasks that are ready to run, and a set of threads which are available to execute an async task. We don't want the set of threads to be too large, but on the other hand we want to keep the available CPU processors busy. It seems like the decision on whether to create a new thread ( due to the CPU processors not being busy, together with the thread set being empty, and the task set being non-empty ) might take up extra processing time, so it's not worth the effort. I don't know, the above is just my mental model, I don't know what practical difficulties arise.
After thinking about the problem a bit more, I think the point is there is no sensible way for an async runtime to decide when to stop creating more threads (if you have long-running or blocking async tasks). It's a decision that probably cannot reasonably be delegated to the async runtime, therefore it's best to have spawn_blocking. A counter argument might be that you could help out programs that fail to use spawn_blocking when it is needed, and perhaps impose some arbitrary limit on the number of threads created, but if the overhead could impact the "well-behaved" programs it could be a questionable feature ( but I guess it could be an optional feature ).