January 2019
Intermediate to advanced
520 pages
14h 32m
English
To run requests in parallel, we'll use the rayon crate, which provides a parallel iterator with the par_iter method. The parallel iterator divides a list into separate tasks that run across a pool of threads:
users.par_iter() .map(|user| -> Result<(), failure::Error> { let conn = pool.get()?; create_user(&conn, &user)?; Ok(()) }) .for_each(drop);
The parallel iterator returns items much like a traditional iterator. We can get a connection from the pool using the Pool::get method, and call the create_user function with a reference to a connection. We also ignore results here, and if any request fails, it will be skipped silently, as in the demonstration, we cannot take care of values that have not been inserted. Since we use multiple ...
Read now
Unlock full access