6

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

I do not know how to load a local file with mio.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
uwu
  • 1,590
  • 1
  • 11
  • 21
  • 4
    The reason you cannot find how to asynchronously read a file with mio is because it is explicitly listed as a non-goal for that project. ^_^ – Shepmaster Dec 18 '15 at 17:33
  • 5
    Can't tell much about Rust, but from an operating system point of view (this is true for the vast majority of, if not all, mainstream systems) creating a thread is by far the best solution. Asynchronous I/O is either implemented poorly, useless, or not working at all on all mainstream operating systems (regardless of what claims they make). – Damon Dec 18 '15 at 17:50
  • You could try `madvise` with `MADV_WILLNEED`. In Rust it'll be in the `libc` crate (https://crates.io/crates/libc/; http://rust-lang-nursery.github.io/libc/x86_64-unknown-linux-gnu/libc/fn.madvise.html). – ArtemGr Dec 18 '15 at 22:42

2 Answers2

4

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);
}
xx1xx
  • 1,834
  • 17
  • 16
1

I would suggest simply spinning off another thread yourself. io is not planning to do this, and making your own async loader allows you to have complete control over how and when reads/writes happen, which is important if performance is your goal (as I would imagine it is, if you need async disk I/O). You can choose whether to write/read single bytes, single lines, or to accumulate blocks and write those. If you application is waiting on something else at other times, like the network, you could choose to write to disk then, for example.

Leonora Tindall
  • 1,391
  • 2
  • 12
  • 30
  • 2
    Could you cite some sources that suggest that **io** from the standard library, is not planning on supporting this? – Shepmaster May 12 '16 at 13:00