#tasks #waiting #collection #group #spawner #tokio

no-std task-collection

Types for managing and waiting on groups of tasks

4 releases

0.0.4 Apr 6, 2022
0.0.3 Dec 15, 2020
0.0.2 Dec 14, 2020
0.0.1 Dec 14, 2020

#870 in Asynchronous

MIT/Apache

11KB
243 lines

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:


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");
    });
}

Dependencies

~0.5–15MB
~137K SLoC