How to read a file asynchronously?

I could create a separate thread to act as an I / O queue, but I'm not sure if this is the best way. It seems like the best.

I do not know how to download a local mio file.

+5
source share
2 answers

I would suggest just unscrew another topic. io no plans to do this, and creating your own asynchronous bootloader allows you to fully control how and when reading / writing occurs, which is important if performance is your goal (as I would suggest if you need an asynchronous I / O disk). You can choose whether to write / read single bytes, single lines or accumulate blocks and write them. If the application at another time expects something else, for example, a network, you can choose, for example, write to disk.

+1
source

Use tokio :: fs :: read:

 use tokio::prelude::Future; fn main() { let task = tokio::fs::read("/proc/cpuinfo").map(|data| { // do something with the contents of the file ... println!("contains {} bytes", data.len()); println!("{:?}", String::from_utf8(data)); }).map_err(|e| { // handle errors eprintln!("IO error: {:?}", e); }); tokio::run(task); } 
0
source

Source: https://habr.com/ru/post/1238656/


All Articles