13 unstable releases (4 breaking)

Uses new Rust 2024

0.5.3 Aug 13, 2025
0.5.1 Jul 30, 2025
0.2.2 Jan 12, 2025
0.2.1 Feb 27, 2024
0.1.0 Oct 21, 2022

#526 in Asynchronous

Download history 31/week @ 2025-12-05 20/week @ 2025-12-12 6/week @ 2025-12-19 1/week @ 2025-12-26 33/week @ 2026-01-02 37/week @ 2026-01-09 49/week @ 2026-01-16 96/week @ 2026-01-23 19/week @ 2026-01-30 33/week @ 2026-02-06 47/week @ 2026-02-13 84/week @ 2026-02-20 146/week @ 2026-02-27 214/week @ 2026-03-06 93/week @ 2026-03-13 114/week @ 2026-03-20

594 downloads per month

MIT/Apache

49KB
945 lines

tokio-tasks

Task managment for tokio

Crates.io License Build status Docs

use tokio_tasks::{TaskBuilder, run_tasks, shutdown, cancelable, RunToken};

// Main task, program will shut down if when finishes
async fn main_task(run_token: RunToken) -> Result<(), String> {
    println!("Main task start");
    match cancelable(&run_token, tokio::time::sleep(std::time::Duration::from_secs(10))).await {
       Ok(()) => println!("Main task finished"),
       Err(_) => println!("Main task cancled"),
    }
    Ok(())
}

// Critical task, program will shut down if this finished with an error
async fn critical_task(run_token: RunToken) -> Result<(), String> {
    println!("Critical task start");
    match cancelable(&run_token, tokio::time::sleep(std::time::Duration::from_secs(1))).await {
       Ok(()) => println!("Critical task finished"),
       Err(_) => println!("Critical task cancled"),
    }
    Ok(())
}

#[tokio::main]
async fn main() {
    TaskBuilder::new("main_task")
        .main()
        .shutdown_order(1)
        .create(|rt| main_task(rt));

    TaskBuilder::new("critical_task")
        .critical()
        .shutdown_order(2)
        .create(|rt| critical_task(rt));

    // Shutdown the application on ctrl+c
    tokio::spawn(async {
        tokio::signal::ctrl_c().await.unwrap();
        shutdown("ctrl+c".to_string());
    });

    // Run until all tasks stop
    run_tasks().await;
}

Dependencies

~2.2–3MB
~48K SLoC