When I use channel to send data from supportive thread to main thread, the function freezes up

I would like to create a function that would read a csv to columns and in this function I have s thread that takes a line of the csv file and gets the column type while the file is being read and I have the following problems:
when I send something from the handle to the main thread or use handle.join(), the function doesn't go further and return output, but just freezes up. How to send data from the supportive thread to the main thread without function being freezed up?

fn read_csv(path:&str)-> Result<(), csv::Error>{
    let mut input = csv::Reader::from_reader(BufReader::new(File::open(path).unwrap()));
    let mut data = HashMap::new();
    //let mut bitvecs = BitVec::new();
    let headers = input.headers()?.clone();
    let mut rows:usize = 0;
    let mut col_count = 0;
    let (num_cols_tx,num_cols_rx):(Sender<usize>, Receiver<usize>) = mpsc::channel();
    let (tx, rx): (Sender<csv::StringRecord>, Receiver<csv::StringRecord>) = mpsc::channel();
    let (dtype_send,dtype_rcv):(Sender<Vec<String>>, Receiver<Vec<String>>) = mpsc::channel();
    for (i,el) in headers.into_iter().enumerate(){

    let get_type_handle = thread::spawn(move||{
        let num_cols = col_count;
        let mut dtypes = HashMap::new();
        let mut data_types = Vec::new();
        for i in 0..num_cols{
        for recieved in rx{
            for i in 0..num_cols{
                let mut k = dtypes.entry(i).or_insert(Vec::new());
                let el = recieved.get(i).map(|s| s.to_string()).unwrap();
               // println!("{:?}",el);
                if !el.parse::<f64>().is_ok(){
                    k[1] = false;
                    k[0] = false;
                    k[3] = false
                if !el.parse::<isize>().is_ok(){
                    k[0] = false;
                    k[1] = false;
                    k[4] = false;
        for i in 0..num_cols{
            let mut k = dtypes.entry(i).or_insert(Vec::new());
            if k[0]==true&&k[1]==false{
                data_types[i] = "float64".to_string();}
            else if k[0]==true&&k[1]==true{
                    data_types[i] = "isize".to_string();
            else if k[0]==false&&k[3]==false{
                data_types[i] = "object".to_string();
                data_types[i] = "category".to_string();

    for line in input.records(){
        rows += 1;
       // println!("{:?}",line);
        let line_un = line.unwrap();
        println!("{:?} {}",line_un,rows);
       // data.push(line?);
    for i in 0..col_count{
        let cur = line_un.get(i).map(|s| s.to_string());
        let mut k = data.entry(i).or_insert(Vec::new());

let data_types = dtype_rcv.recv().unwrap();
   // return Ok(DataFrame::from_hash_map(&columns));


From quick read your code seems blocked at for recieved in rx {. It will wait for the next send while any sender exists.

Drop the sender tx once you have done; the loop then finishes after outstanding receives complete.

1 Like

Thanks, I will try