Coder Social home page Coder Social logo

dapper91 / coachman-rs Goto Github PK

View Code? Open in Web Editor NEW
0.0 3.0 0.0 135 KB

rust asynchronous task manager built on top of tokio framework supporting task cancellation feature

Home Page: https://docs.rs/coachman

License: The Unlicense

Rust 100.00%
rust rust-lang tokio task-manager cancellable supervisor async asynchronous

coachman-rs's Introduction

CoachMan

coachman

coachman is a rust asynchronous task manager built on top of tokio framework.

Features

  • Task count control: coachman allows you to control task count preventing your application from uncontrolled task count explosion.
  • Task cancellation: The main feature of coachman is task cancellation. It provides a simple api for making your task cancelable.

Basic example

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!(),
           }
       }
   }
}

coachman-rs's People

Contributors

dapper91 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.