Crates.io | coachman |
lib.rs | coachman |
version | 0.3.0 |
source | src |
created_at | 2021-11-14 17:36:52.997194 |
updated_at | 2021-11-21 08:43:55.459101 |
description | rust asynchronous task manager built on top of tokio framework |
homepage | https://github.com/dapper91/coachman-rs |
repository | https://github.com/dapper91/coachman-rs |
max_upload_size | |
id | 481788 |
size | 99,317 |
coachman
is a rust asynchronous task manager built on top of tokio framework.
coachman
allows you to control task count preventing your application from uncontrolled task count explosion.coachman
is task cancellation. It provides a simple api for making your task cancelable.The main feature of coachman is making asynchronous tasks cancelable.
Look at the following example:
use coachman as cm;
use coachman::{try_await, Canceled, Completed, TaskError};
async fn inner_func(i: usize, duration: u64) {
match try_await!(tokio::time::sleep(std::time::Duration::from_secs(duration))) {
Canceled => println!("task#{} inner canceled", i),
Completed(_) => println!("task#{} inner completed", i),
}
}
async fn outer_func(i: usize, duration: u64) {
match try_await!(inner_func(i, duration)) {
Canceled => println!("task#{} outer canceled", i),
Completed(_) => println!("task#{} outer completed", i),
}
}
#[tokio::main(flavor = "current_thread")]
async fn main() {
let mut task_handles = Vec::new();
for i in 0..5 {
let duration = i as u64;
task_handles.push(cm::spawn(outer_func(i, duration)));
}
let deadline = tokio::time::Instant::now() + std::time::Duration::from_secs(2);
for (i, mut handle) in task_handles.into_iter().enumerate() {
if tokio::time::timeout_at(deadline, &mut handle).await.is_ok() {
println!("task-{} completed", i);
} else {
handle.cancel();
match handle.await {
Result::Err(TaskError::Canceled) => println!("task-{} canceled", i),
Result::Err(TaskError::Aborted) => println!("task-{} aborted", i),
Result::Err(TaskError::Panicked(_)) => println!("task-{} panicked", i),
Result::Ok(_) => unreachable!(),
}
}
}
}