I have the following code
main.rs
use futures::executor::block_on;
use futures::join;
use std::io;
mod file;
fn main() -> io::Result<()> {
block_on(load_files());
Ok(())
}
async fn load_files() {
join!(load_file_1(), load_file_2());
}
async fn load_file_1() {
let r1 = file::read_file("src/file1.txt").await;
println!("file 1 size: {}", r1.unwrap().len());
}
async fn load_file_2() {
let r2 = file::read_file("src/file2.txt").await;
println!("file 2 size: {}", r2.unwrap().len());
}
file.rs
use std::fs::File;
use std::io::{self, Read};
pub async fn read_file(path: &str) -> io::Result<String> {
let mut file = File::open(path)?;
let mut buffer = String::new();
file.read_to_string(&mut buffer)?;
Ok(buffer)
}
My understanding is that by joining load_file_1 and load_file_2, they should execute concurrently. However, this is not the case. Putting a huge file1.txt file (around 5Gb), the program still blocks on load_file_1. This is not the case if I'm using async_std task::spawn function.
Is the problem in my code or in join!?