#tokio #task #safe #pool


Task pool for Tokio Runtime

6 releases

0.1.5 Sep 14, 2023
0.1.4 Sep 14, 2023

#1019 in Network programming

Download history 145/week @ 2023-09-10 25/week @ 2023-09-17 36/week @ 2023-09-24 19/week @ 2023-10-01 44/week @ 2023-10-08 39/week @ 2023-10-15 97/week @ 2023-10-22 48/week @ 2023-10-29 39/week @ 2023-11-05 69/week @ 2023-11-12 124/week @ 2023-11-19

286 downloads per month
Used in 7 crates (via busrt)

MIT license

300 lines


Task pool for Tokio Runtime


The problem

A typical pattern

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

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.


  • 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;

async fn main() {
    let pool = Pool::bounded(5)
    pool.spawn(async move {
        // do some job

More tricks

Refer to the crate documentation.


  • A "log" feature enables automatic error logging for timed out tasks (via the log crate)


~56K SLoC