Conflict about 'write' and 'read' a same file in 'future::stream'

when use multiple process by future::stream, I want to communicate data between each process and the data can be write and read, now I don't confirm the below confiction can occur or not.

I also consider another method which I defined a mut web_data(init as blank) on main function; and pass the mut web_data to all process; only one process write data into web_data and other processes to use web_data when check it is not 'blank' again.

use futures::{TryFutureExt, TryStreamExt};
use std::error::Error;
use std::{thread, time, vec};


async fn run_once(unique_num: f32, save_file: &str, write: bool) -> Result<f32, Box<dyn Error>> {
    let mut web_data: Vec<f64> = vec![];
    if write {
        web_data = ...// request a big data from web;
        // here write `web_data` into `save_file`;
    } else {
        loop {
            if std::path::Path::new(save_file).exists() {
                web_data = ....// read from `save_file`; here whether there is 
                               // a chance for confilct that when `save_file` exists 
                               // but have not been written completely, 
                               // now here is reading a uncompleted file, error occurs?
                break;
            } else {
                // sleep 1 seconds
            }
        }
        }
    Ok(unique_num)
}


#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let save_file = "/root/tmp";
    let unique_num_list = vec![4.0, 2.0, 3.0, 1.0];
    let futures_threads = futures::stream::FuturesOrdered::from_iter((0..unique_num_list.len()).map(|i| {
        let unique_numi = unique_num_list[i];
        let mut write = false;
        if i == 0{
            write = true;
        }
        run_once(unique_numi, save_file, write)}));
    let preds_list: Vec<f32> = futures_threads.try_collect().await?;
    dbg!(preds_list);

    Ok(())
}

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.