#io #async #non-blocking #futures #wasi

tokio_wasi

An event-driven, non-blocking I/O platform for writing asynchronous I/O backed applications

6 stable releases

1.25.2 Jun 6, 2023
1.25.1 Feb 11, 2023
1.23.0 Jan 3, 2023
1.21.3 Nov 8, 2022
1.21.2 Sep 30, 2022

#44 in Asynchronous

Download history 638/week @ 2023-11-02 511/week @ 2023-11-09 683/week @ 2023-11-16 622/week @ 2023-11-23 441/week @ 2023-11-30 514/week @ 2023-12-07 561/week @ 2023-12-14 634/week @ 2023-12-21 497/week @ 2023-12-28 702/week @ 2024-01-04 2528/week @ 2024-01-11 6707/week @ 2024-01-18 7493/week @ 2024-01-25 8418/week @ 2024-02-01 8107/week @ 2024-02-08 4797/week @ 2024-02-15

29,253 downloads per month
Used in 36 crates (27 directly)

MIT license

2.5MB
36K SLoC

Tokio for WebAssembly

A runtime for writing reliable, asynchronous, and slim applications with the Rust programming language. This is a fork of the original tokio so that it can be compiled into WebAssembly. The WebAssembly app can run inside the WasmEdge Runtime as a lightweight and secure alternative to natively compiled apps in Linux container.

  • Fast: Tokio's zero-cost abstractions give you bare-metal performance.

  • Reliable: Tokio leverages Rust's ownership, type system, and concurrency model to reduce bugs and ensure thread safety.

  • Scalable: Tokio has a minimal footprint, and handles backpressure and cancellation naturally.

Overview

Tokio is an event-driven, non-blocking I/O platform for writing asynchronous applications with the Rust programming language. At a high level, it provides a few major components:

  • A multithreaded, work-stealing based task scheduler.
  • A reactor backed by the operating system's event queue (epoll, kqueue, IOCP, etc...).
  • Asynchronous TCP and UDP sockets.

These components provide the runtime components necessary for building an asynchronous application.

Example

A basic TCP echo server with Tokio.

Make sure you activated the full features of the tokio crate on Cargo.toml:

[dependencies]
tokio_wasi = { version = "1.25", features = ["full"] }

Then, on your main.rs:

use tokio::net::TcpListener;
use tokio::io::{AsyncReadExt, AsyncWriteExt};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let listener = TcpListener::bind("127.0.0.1:8080").await?;

    loop {
        let (mut socket, _) = listener.accept().await?;

        tokio::spawn(async move {
            let mut buf = [0; 1024];

            // In a loop, read data from the socket and write the data back.
            loop {
                let n = match socket.read(&mut buf).await {
                    // socket closed
                    Ok(n) if n == 0 => return,
                    Ok(n) => n,
                    Err(e) => {
                        eprintln!("failed to read from socket; err = {:?}", e);
                        return;
                    }
                };

                // Write the data back
                if let Err(e) = socket.write_all(&buf[0..n]).await {
                    eprintln!("failed to write to socket; err = {:?}", e);
                    return;
                }
            }
        });
    }
}

More examples can be found here. For a larger "real world" example, see the mini-redis repository.

License

This project is licensed under the MIT license.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in Tokio by you, shall be licensed as MIT, without any additional terms or conditions.

Dependencies

~0–11MB
~100K SLoC