Crates.io | async-task-group |
lib.rs | async-task-group |
version | 0.2.1 |
source | src |
created_at | 2021-10-25 10:06:47.75597 |
updated_at | 2021-10-25 10:06:47.75597 |
description | Manage groups of `async-std` tasks as a single unit. |
homepage | |
repository | https://github.com/yoshuawuyts/async-task-group |
max_upload_size | |
id | 470896 |
size | 35,392 |
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?;
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(())
}
$ cargo add async-task-group
This crate uses #![deny(unsafe_code)]
to ensure everything is implemented in
100% Safe Rust.
Want to join us? Check out our "Contributing" guide and take a look at some of these issues: