pub async fn with_spawn_group<Closure, Fut, ResultType, ReturnType>(
body: Closure,
) -> ReturnTypeExpand description
Starts a scoped closure that takes a mutable SpawnGroup instance as an argument which can execute any number of child tasks which its result values are of the generic ResultType type.
This closure ensures that before the function call ends, all spawned child tasks are implicitly waited for, or the programmer can explicitly wait by calling its wait_for_all() method
of the SpawnGroup struct.
This function use a threadpool of the same number of threads as the number of active processor count that is default amount of parallelism a program can use on the system for polling the spawned tasks
See SpawnGroup
for more.
§Parameters
body: an async closure that takes a mutable instance ofSpawnGroupas an argument
§Returns
Anything the body parameter returns
§Example
use spawn_groups::GetType;
use spawn_groups::with_spawn_group;
use futures_lite::StreamExt;
use spawn_groups::Priority;
let final_result = with_spawn_group(|mut group| async move {
for i in 0..=10 {
group.spawn_task(Priority::default(), async move {
// simulate asynchronous operation
i
});
}
group.fold(0, |acc, x| {
acc + x
}).await
}).await;
assert_eq!(final_result, 55);