Crate async_task_group[][src]

Expand description

Manage a group of tasks on a runtime.

Enables cancellation to be propagated across tasks, and ensures if an error occurs that all sibling tasks in the group are cancelled too.

Closures and References

When calling group, a GroupHandle instance is passed into the closure. The intended design is that the instance of GroupHandle never outlives the closure it’s contained within. This makes it so when the TaskGroup exits or is cancelled, it is also no longer possible to spawn more tasks on the group.

However async closures are not yet a primitive in Rust, so we cannot yet pass GroupHandle by-reference. Instead we approximate the borrowing behavior by requiring the GroupHandle instance be returned at the end of the closure.


This codebase is based on the task-group project by Pat Hickey.


Create an echo tcp server which processes incoming connections in a loop without ever creating any dangling tasks:

use async_std::io;
use async_std::net::{TcpListener, TcpStream};
use async_std::prelude::*;
use async_std::task;

async fn process(stream: TcpStream) -> io::Result<()> {
    println!("Accepted from: {}", stream.peer_addr()?);

    let mut reader = stream.clone();
    let mut writer = stream;
    io::copy(&mut reader, &mut writer).await?;


async fn main() -> io::Result<()> {
    let listener = TcpListener::bind("").await?;
    println!("Listening on {}", listener.local_addr()?);

    let handle = async_task_group::group(|group| async move {
        let mut incoming = listener.incoming();
        while let Some(stream) = {
            let stream = stream?;
            group.spawn(async move { process(stream).await });


Task builder that configures the settings of a new task

A JoinHandle is used to manage a collection of tasks. There are two things you can do with it:

A TaskGroup is used to spawn a collection of tasks. The collection has two properties:


Create a new instance.