#async-std #async-task #task #async #spawn #future

async-task-group

Manage groups of async-std tasks as a single unit

1 unstable release

0.2.1 Oct 25, 2021

#2008 in Asynchronous

Download history 2/week @ 2024-01-07 89/week @ 2024-01-21 73/week @ 2024-01-28 1/week @ 2024-02-04 1/week @ 2024-02-11 52/week @ 2024-02-18 31/week @ 2024-02-25 17/week @ 2024-03-03 18/week @ 2024-03-10 7/week @ 2024-03-17 9/week @ 2024-03-24 19/week @ 2024-03-31

58 downloads per month

MIT/Apache

18KB
208 lines

async-task-group

Manage groups of `async-std` tasks as a single unit.

Credit

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

Examples

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?;

    Ok(())
}

#[async_std::main]
async fn main() -> io::Result<()> {
    let listener = TcpListener::bind("127.0.0.1:8080").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) = incoming.next().await {
            let stream = stream?;
            group.spawn(async move { process(stream).await });
        }
        Ok(group)
    });
    handle.await?;
    Ok(())
}

Installation

$ cargo add async-task-group

Safety

This crate uses #![deny(unsafe_code)] to ensure everything is implemented in 100% Safe Rust.

Contributing

Want to join us? Check out our "Contributing" guide and take a look at some of these issues:

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~5–16MB
~185K SLoC