Loading AI tools
Library for Rust programming language From Wikipedia, the free encyclopedia
Tokio is a software library for the Rust programming language. It provides a runtime and functions that enable the use of asynchronous I/O, allowing for concurrency in regards to task completion.[2][3][4]
Original author(s) | Carl Lerche |
---|---|
Initial release | December 23, 2020 |
Stable release | |
Repository | |
Written in | Rust |
Operating system | macOS, Windows, Linux, FreeBSD, WebAssembly |
Type | Asynchronous runtime |
License | MIT License |
Website | tokio |
Tokio was released in August 2016 for Rust, a general-purpose programming language. Developed by Carl Lerche, Tokio began as a network application framework and supports features such as socket listening and broadcasting, allowing messages to be transferred between computers.
Tokio began in August 2016 by Carl Lerche as a network application framework for Rust built on futures, allowing for network-based middleware and a non-blocking, or asynchronous, implementation of readiness interest to the reactor. Tokio was inspired by Finagle, a Scala-based asynchronous remote procedure call (RPC) system developed at Twitter for Java virtual machines (JVM), allowing distributed systems to communicate within a JVM. Tokio utilizes the lower-level Rust crate mio
, itself using system calls such as epoll (Linux), kqueue (FreeBSD), and the input/output completion port (IOCP) API (Windows). For Linux it can also use io_uring via tokio-uring.[5][6][7] The name "Tokio" is derived from Tokyo and mio.[8] The preliminary version of Tokio was released in January 2017,[9] followed by a full release in December 2020.[10][11] In 2017, Tokio received a grant from the Mozilla Open Source Support fund.[12] In April 2021, Tokio funded its first paid contributor, Alice Ryhl, for her work both developing the project and assisting its users.[13][14]
While Rust has supported asynchronous functions since version 1.39, released in November 2019,[15] it provides no facilities to execute them, requiring an external runtime for that purpose.[16] Tokio provides a runtime that uses a multi-threaded work stealing scheduler.[10] Rust's futures are lazily evaluated, requiring functions to call .await
before they do any work.[17] When .await
is invoked, Tokio's runtime may pause the original future until its I/O completes, and unpauses a different task that is ready for further processing.[18]
Users of Tokio include the development teams behind Discord and AWS Lambda.[10] The JavaScript and TypeScript runtime Deno uses Tokio under the hood, in comparison to the JavaScript runtime Node.js, which uses the libuv library.[19]
Tokio allows for the usage of asynchronous functions in Rust through the creation of an asynchronous runtime. This can be accomplished through the #[tokio::main]
macro.[18]
For example:
#[tokio::main]
async fn main() -> Result<()> {
let url = "https://en.wikipedia.org/";
let text = reqwest::get(url).await?.text().await?;
println!("{}", text);
Ok(())
}
In this example, the reqwest
crate is used to request the HyperText Markup Language (HTML) for English Wikipedia. To ensure that the request is not immediately handled, Tokio wraps the function call into an asynchronous runtime, waiting for the request to complete before calling println()
.
Tokio also includes a version of the Rust standard library that is designed for being used asynchronously. For example, tokio::fs::read_to_end()
, which reads the contents of a file, is the asynchronous version of std::fs::read_to_end()
.[20] In addition, Tokio supports io_uring, a Linux asynchronous I/O syscall interface, in a separate crate named tokio-uring
.[10][21]
Tokio further allows users to create tasks, which are stackless coroutines, using a tokio::spawn()
function. Tasks run at the user level, providing concurrency even when there is only a single thread.[22]
Tokio is capable of listening on a socket through a non-blocking approach.[5] In particular, the TcpListener
structure binds a Transmission Control Protocol (TCP) socket listener to an address and asynchronously executes function.[23]
Tokio provides a broadcast channel type, allowing for messages to be broadcast to multiple receivers. Upon sending a message, it is received by such receivers. This enables real-time communication and distributed systems, among other applications.[24]
Seamless Wikipedia browsing. On steroids.
Every time you click a link to Wikipedia, Wiktionary or Wikiquote in your browser's search results, it will show the modern Wikiwand interface.
Wikiwand extension is a five stars, simple, with minimum permission required to keep your browsing private, safe and transparent.