Skip to content

question about blocking action? #2417

@zhuxiujia

Description

@zhuxiujia

If I want to run a blocking action, do I need to manually differentiate? Can you automatically To deal with?

async fn read_to_string(path: impl AsRef<Path>) -> io::Result<String> {
task::spawn_blocking(move || {
 // to do some thing block io
  std::fs::read_to_string(path)
});
}

allow replace to Auto deal with

async fn read_to_string(path: impl AsRef<Path>) -> io::Result<String> {
  std::fs::read_to_string(path)
}

Because there are many libraries many IO operations are blocked for historical reasons(Even rust Std libraries)
If you use manual differentiation, you'll end up with a huge amount of code。

“async_std” this crate can auto(Inspired by golang), for example doc.
https://async.rs/blog/stop-worrying-about-blocking-the-new-async-std-runtime/

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-tokioArea: The main tokio crateC-questionUser questions that are neither feature requests nor bug reportsM-blockingModule: tokio/task/blocking

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions