#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

#2318 in Asynchronous

47 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

~4–13MB
~165K SLoC