tokio-mpmc

Crates.iotokio-mpmc
lib.rstokio-mpmc
version0.2.4
created_at2025-05-19 23:08:52.848689+00
updated_at2025-06-10 06:33:11.261059+00
descriptionA multi-producer multi-consumer queue implementation based on Tokio
homepage
repositoryhttps://github.com/lispking/tokio-mpmc
max_upload_size
id1680526
size221,176
King (lispking)

documentation

README

tokio-mpmc

Crates.io Documentation License: Apache 2.0 build status Ask DeepWiki

A high-performance multi-producer multi-consumer (MPMC) queue implementation based on Tokio.

architecture

Features

  • Asynchronous implementation based on Tokio
  • Support for multi-producer multi-consumer pattern
  • Message processing using consumer pool
  • Simple and intuitive API
  • Complete error handling
  • Queue capacity control

Installation

Add the following dependency to your Cargo.toml:

[dependencies]
tokio-mpmc = "0.2"

Usage Example

Queue

use tokio_mpmc::Queue;

#[tokio::main]
async fn main() {
    // Create a queue with capacity of 100
    let queue = Queue::new(100);

    // Send a message
    if let Err(e) = queue.send("Hello").await {
        eprintln!("Send failed: {}", e);
    }

    // Receive a message
    match queue.receive().await {
        Ok(Some(msg)) => println!("Received message: {}", msg),
        Ok(None) => println!("Queue is empty"),
        Err(e) => eprintln!("Receive failed: {}", e),
    }

    // Close the queue
    drop(queue);
}

Channel

use tokio_mpmc::channel;

#[tokio::main]
async fn main() {
    // Create a channel with capacity of 100
    let (tx, rx) = channel(100);

    // Send a message
    if let Err(e) = tx.send("Hello").await {
        eprintln!("Send failed: {}", e);
    }

    // Receive a message
    match rx.recv().await {
        Ok(Some(msg)) => println!("Received message: {}", msg),
        Ok(None) => println!("Channel is closed"),
        Err(e) => eprintln!("Receive failed: {}", e),
    }

    // Close the channel
    drop(tx);
}

Performance

cargo criterion --message-format=json | criterion-table > BENCHMARKS.md

Benchmark Results

tokio-mpsc-channel tokio-mpmc-channel tokio-mpmc-queue flume
non-io 1.39 ms (✅ 1.00x) 65.38 us (🚀 21.21x faster) 168.86 us (🚀 8.21x faster) 773.68 us (✅ 1.79x faster)
io 197.97 ms (✅ 1.00x) 46.32 ms (🚀 4.27x faster) 46.83 ms (🚀 4.23x faster) 197.76 ms (✅ 1.00x faster)

Note: non-io means no IO operation, io means IO operation.

See benchmark code

License

This project is licensed under the Apache-2.0 License. See the LICENSE file for details.

Commit count: 22

cargo fmt