Expand description
Rayon does not natively support interrupting long-running computations. This crate provides an iterator adapter for rayon that can be interrupted during computation.
The provided CancelAdapter can be used on any rayon iterator and can be used to interrupt processing new items at any given point.
The adapter provides a handle to cancel the computation and a handle to access the number of processed items.
By design, the adapter cannot interrupt processing individual items. Once the computation is cancelled, the adapter will stop producing or consuming new items. Which items are processed before the computation stops is non-deterministic and depends on the way rayon distributes the work.
Using this adapter may be less efficient than using the underlying iterator directly as the number of items produced by the iterator cannot be known in advance.
If you only need access to the number of processed items, you may want to have a look at the rayon-progress crate.
§Example
use rayon::prelude::*;
use rayon_cancel::CancelAdapter;
let adapter = CancelAdapter::new(0..100000);
let canceller = adapter.canceller();
let progress = adapter.counter();
std::thread::spawn(move || {
while progress.get() < 1000 {
std::thread::sleep(std::time::Duration::from_millis(2));
}
canceller.cancel();
});
let count = adapter.counter();
// some expensive computation
let processed: Vec<_> = adapter.filter(|_| true).map(|i| {
std::thread::sleep(std::time::Duration::from_millis(20));
i
}).collect();
assert!(count.get() > 1000);
assert!(count.get() < 100000);
// `processed` contains `count` items, but which ones is non-deterministic
assert_eq!(processed.len(), count.get());Structs§
- Cancel
Adapter - Iterator adapter that can be interrupted during computation.
- Cancel
Handle - Handle that allows cancelling a running iterator computation.
- Count
Handle - Access the number of items processed by the iterator.