9 releases

0.1.8 Oct 18, 2025
0.1.7 Oct 16, 2025
0.1.6 Sep 15, 2025
0.1.5 Sep 14, 2023

#278 in Asynchronous

Download history 151/week @ 2025-08-18 340/week @ 2025-08-25 236/week @ 2025-09-01 411/week @ 2025-09-08 778/week @ 2025-09-15 398/week @ 2025-09-22 283/week @ 2025-09-29 139/week @ 2025-10-06 397/week @ 2025-10-13 302/week @ 2025-10-20 332/week @ 2025-10-27 260/week @ 2025-11-03 206/week @ 2025-11-10 164/week @ 2025-11-17 162/week @ 2025-11-24 137/week @ 2025-12-01

697 downloads per month
Used in 11 crates (2 directly)

MIT license

17KB
372 lines

tokio-task-pool

Task pool for Tokio Runtime

https://crates.io/crates/tokio-task-pool

The problem

A typical pattern

loop {
    let (socket, _) = listener.accept().await.unwrap();
    tokio::spawn(async move {
        process(socket).await;
    });
}

is actually an anti-pattern which may break your production.

Why? Because this pattern behaves equally to an unbounded channel. If the producer has higher rate than consumers, it floods runtime with tasks and sooner or later causes memory overflow.

Solution

  • Use a pool of workers instead

  • Use task spawning but manually limit the number of active tasks with a semaphore

  • Use this crate which does the job out-of-the-box

Features provided

  • Pool objects with safe spawn methods, which automatically limit number of tasks

  • Tasks can be automatically aborted if run timeout is set, global or per task

Code example

Simple spawning is pretty similar to tokio::spawn, but async because the producer must be blocked until there is an empty task slot in the pool:

use std::time::Duration;
use tokio_task_pool::Pool;

#[tokio::main]
async fn main() {
    let pool = Pool::bounded(5)
        .with_spawn_timeout(Duration::from_secs(5))
        .with_run_timeout(Duration::from_secs(10));
    pool.spawn(async move {
        // do some job
    }).await.unwrap();
}

More tricks

Refer to the crate documentation.

Features

  • log: enables automatic error logging for timed out tasks via the log crate

  • tracing: like log, but uses the tracing crate

Dependencies

~1.8–5MB
~82K SLoC