Parallel imports with the rayon crate

Since we have a pool of connections, we can run multiple requests to a database in parallel. We will read users from a standard input stream in CSV format. Let's add a branch to the match expression we declared before, for the import subcommand, and open stdin with csv::Reader. After that, we will use the deserialize method of the reader, which returns an iterator of deserialized instances to our desired type. In our case, we deserialize the CSV data to a list of User structs and push them to a vector:

(CMD_IMPORT, _) => {
let mut rdr = csv::Reader::from_reader(io::stdin());
let mut users = Vec::new();
for user in rdr.deserialize() {
users.push(user?);
}
// Put parallel statements execution here
}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.143.0.85