this is my code: use bio::io::bed; use crossbeamutils::thread::scope; use…
- User
this is my code:
use bio::io::bed; use crossbeam_utils::thread::scope; use std::io::{self, Read}; use std::sync::{mpsc, Arc, Mutex};
fn main() { let mut reader = bed::Reader::from_file( "/home/alejandro/Documents/unam/TOGA_old_versions/x/bed_tmp/100k.bed", ) .unwrap();
// define the number of messages the channel can hold let num_threads = 6; // create a synchronous channel with a bounded capacity let (tx, rx) = mpsc::sync_channel(num_threads); let atomic_reader = Arc::new(Mutex::new(reader.records())); // spawn threads to send messages scope(|scope| { for i in 0..num_threads { let tx = tx.clone(); let reader = Arc::clone(&atomic_reader); scope.spawn(move |_| { println!("Thread {:?} started", i); loop { match get_next_record(&reader) { Some(record) => { let record = record.expect("Error reading record"); let tx_id = record.start(); tx.send(Some(tx_id)).expect("Could not send data"); } None => { tx.send(None).expect("Could not send data"); break; } } } // loop }); // end spawn } // end for }); // end scope // receive messages let mut death_txs = 0; let mut received = 0; for rc in rx.iter() { match rc { Some(record) => { // println!("{:?}", record); received += 1; eprint!("\rRecords received: {}", received); } None => { death_txs += 1; if death_txs == num_threads { drop(tx); break; } } } }}
pub fn get_next_record<R: io::Read>( reader: &Arc<Mutex<bed::Records>>, ) -> Option<Result<bed::Record, csv::Error>> { let mut lock = reader.lock().unwrap(); lock.next() }
and this is the output:
Thread 0 started Thread 1 started Thread 2 started Thread 3 started Thread 4 started Thread 5 started
nothing else, is stuck. You are an expert in Rust, please help me
- Assistant
- User
error[E0599]: no method named
nextfound for reference&MutexGuard<'_, bio::io::bed::Reader<File>>in the current scope --> src/main.rs:29:46 | 29 | while let Some(record) = records.next() { | ^^^^ method not found in&MutexGuard<'_, Reader<File>> - Assistant
Apologies for the confusion. The issue is arising because I overlooked the fact that you were locking the
Mutexand attempting to callnextdirectly on theMutexGuardrather than the iterator.Since the
bio::io::bed::Recordsis an iterator, we need to obtain thenextelement on it, rather than on theMutexGuard. Here's a revised version of your spawning threads part that locks the mutex, obtains the next element from the iterator, and then releases the lock each time: - User
what is the difference between this approach and just using "par_iter()"
- Assistant
- User
which one do you think is going to be faster?
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.