task-collection 0.0.4

Types for managing and waiting on groups of tasks
Documentation

Types for spawning and waiting on groups of tasks.

This crate provides onthe TaskCollection for the grouping of spawned tasks. The TaskCollection type is created using a Spawner implementation. Any tasks spawned via the TaskCollections spawn method are tracked with minimal overhead. awaiting the TaskCollection will yield until all the spawned tasks for that collection have completed.

The following example shows how to use a TaskCollection to wait for spawned tasks to finish:

# use task_collection::TaskCollection;
# use futures::channel::mpsc;
# use futures::StreamExt;
# use core::time::Duration;

fn main() {
let runtime = tokio::runtime::Runtime::new().unwrap();
let (tx, mut rx) = mpsc::unbounded::<u64>();

runtime.spawn(async move {
(0..10).for_each(|v| {
tx.unbounded_send(v).expect("Failed to send");
})
});

runtime.block_on(async {
let collection = TaskCollection::new(&runtime);
while let Some(val) = rx.next().await {
collection.spawn(async move {
// Simulate some async work
tokio::time::sleep(Duration::from_secs(val)).await;
println!("Value {}", val);
});
}

collection.await;
println!("All values printed");
});
}