Tokio (software)

{{Short description|Library for Rust programming language}}

{{Infobox software

| title = Tokio

| name = Tokio

| logo = Tokio logo.svg

| logo size = 200px

| logo alt =

| logo caption =

| screenshot =

| screenshot size =

| screenshot alt =

| caption =

| collapsible =

| author = Carl Lerche

| developer =

| released = {{Start date and age|2020|12|23}}

| discontinued =

| latest release version = {{wikidata|property|edit|reference|P348}}

| latest release date =

| latest preview version =

| latest preview date =

| programming language = Rust

| operating system = macOS, Windows, Linux, FreeBSD, WebAssembly

| size =

| language =

| language count =

| language footnote =

| genre = Asynchronous runtime

| license = MIT License

| alexa =

| standard =

| AsOf =

| website = {{URL|https://tokio.rs/}}

}}

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.{{Cite book|last=Chanda|first=Abhishek|url=https://www.worldcat.org/oclc/1028194311|title=Network Programming with Rust: Build fast and resilient network servers and clients by leveraging Rust's memory-safety and concurrency features.|date=2018|publisher=Packt Publishing|isbn=978-1-78862-171-7|location=Birmingham|oclc=1028194311}}{{Cite book|last=Sharma|first=Rahul|url=https://www.worldcat.org/oclc/1090681119|title=Mastering Rust : learn about memory safety, type system, concurrency, and the new features of Rust 2018 edition|date=2019|others=Vesa Kaihlavirta|isbn=978-1-78934-118-8|edition=Second|location=Birmingham, UK|oclc=1090681119}}{{Cite web|last=De Simone|first=Sergio|date=2021-01-06|title=Rust Asynchronous Runtime Tokio Reaches 1.0|url=https://www.infoq.com/news/2021/01/rust-tokio-async-runtime/|access-date=2021-11-21|website=InfoQ|language=en}}

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.

History

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 [https://github.com/tokio-rs/tokio-uring tokio-uring].{{Cite web |url=https://medium.com/@carllerche/announcing-tokio-df6bb4ddb34 |title=Announcing Tokio |date=August 3, 2016 |last=Lerche |first=Carl |access-date=December 11, 2022}}{{Cite web |url=https://blog.twitter.com/engineering/en_us/a/2011/finagle-a-protocol-agnostic-rpc-system |title=Finagle: A Protocol-Agnostic RPC System |date=August 19, 2011 |access-date=December 11, 2022}}{{Cite book|last1=Gomez|first1=Guillaume|last2=Boucher|first2=Antoni|url=https://books.google.com/books?id=qNRJDwAAQBAJ|title=Rust Programming By Example: Enter the World of Rust by Building Engaging, Concurrent, Reactive, and Robust Applications|date=2018|publisher=Packt Publishing|isbn=9781788470308|location=Birmingham}} The name "Tokio" is derived from Tokyo and mio.{{Cite web |url=https://www.reddit.com/r/rust/comments/4vzomj/announcing_tokio_a_finagle_inspired_network/d62svqf/ |title=I enjoyed visiting Tokio (Tokyo) the city and I liked the "io" suffix and how it plays w/ Mio as well. I don't know... naming is hard so I didn't spend too much time thinking about it. |date=August 3, 2016 |last=Lerche |first=Carl |publisher=Reddit |access-date=December 11, 2022}} The preliminary version of Tokio was released in January 2017,{{Cite web |url=https://tokio.rs/blog/2017-01-tokio-0-1 |title=Announcing Tokio 0.1 |last1=Lerche |first1=Carl |last2=Crichton |first2=Alex |last3=Turon |first3=Aaron |access-date=December 11, 2022}} followed by a full release in December 2020.{{Cite web|last=Krill|first=Paul|date=2021-01-08|title=Tokio Rust runtime reaches 1.0 status|url=https://www.infoworld.com/article/3602776/tokio-rust-runtime-reaches-10-status.html|access-date=2021-09-03|website=InfoWorld|language=en}}{{Cite web |url=https://tokio.rs/blog/2020-12-tokio-1-0 |title=Announcing Tokio 1.0 |last=Lerche |first=Carl |access-date=December 11, 2022}} In 2017, Tokio received a grant from the Mozilla Open Source Support fund.{{Cite web|title=Mozilla Awards $365,000 to Open Source Projects as part of MOSS|url=https://lwn.net/Articles/719632/|access-date=2021-11-21|website=LWN.net}} In April 2021, Tokio funded its first paid contributor, Alice Ryhl, for her work both developing the project and assisting its users.{{Cite web|title=Welcoming Alice Ryhl as the first paid Tokio contributor|url=https://tokio.rs/blog/2021-04-welcome-alice|access-date=2021-11-28|website=Tokio}}{{Cite podcast|url=https://rustacean-station.org/episode/046-alice-ryhl/|title=Tokio Ecosystem with Alice Ryhl|website=Rustacean Station|host=Allen Wyma|date=12 November 2021|access-date=2021-11-26}}

While Rust has supported asynchronous functions since version 1.39, released in November 2019,{{Cite web|title=Rust Gets Zero-Cost Async/Await Support in Rust 1.39|url=https://www.infoq.com/news/2019/11/rust-async-await/|access-date=2021-11-28|website=InfoQ|language=en}} it provides no facilities to execute them, requiring an external runtime for that purpose.{{Cite web|title=The Async Ecosystem|url=https://rust-lang.github.io/async-book/08_ecosystem/00_chapter.html|access-date=2021-11-28|website=Asynchronous Programming in Rust}} Tokio provides a runtime that uses a multi-threaded work stealing scheduler. Rust's futures are lazily evaluated, requiring functions to call .await before they do any work.{{Cite web|last=Matsakis|first=Niko|date=2019-11-07|title=Async-await on stable Rust!|url=https://blog.rust-lang.org/2019/11/07/Async-await-stable.html|access-date=2021-11-28|website=Rust Blog|language=en}} 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.{{Cite web|title=Hello Tokio|url=https://tokio.rs/tokio/tutorial/hello-tokio|access-date=2021-11-28|website=Tokio}}

Users of Tokio include the development teams behind Discord and AWS Lambda. The JavaScript and TypeScript runtime Deno uses Tokio under the hood, in comparison to the JavaScript runtime Node.js, which uses the libuv library.{{Cite book|last=Rappl Moraza|first=Florian|url=https://books.google.com/books?id=HyqdEAAAQBAJ|title=Modern Frontend Development with Node.js: A Compendium for Modern JavaScript Web Development Within the Node.js Ecosystem|date=2022|isbn=9781804617380|location=Birmingham, UK}}

Features

=Asynchronous code=

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.

For example:

  1. [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().{{Cite web|title=I/O|url=https://tokio.rs/tokio/tutorial/io|access-date=December 11, 2022|website=Tokio}} In addition, Tokio supports io_uring, a Linux asynchronous I/O syscall interface, in a separate crate named tokio-uring.{{Cite web|title=Announcing tokio-uring: io-uring support for Tokio|url=https://tokio.rs/blog/2021-07-tokio-uring|access-date=2021-11-28|website=Tokio}}

=The coroutine model=

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.{{Cite web |url=https://www.infoworld.com/article/2077383/four-for-the-ages.html |title=Four for the ages |date=April 13, 2001 |last=Sintes |first=Tony |work=InfoWorld |access-date=January 5, 2023}}

=Socket listening=

Tokio is capable of listening on a socket through a non-blocking approach. In particular, the TcpListener structure binds a Transmission Control Protocol (TCP) socket listener to an address and asynchronously executes function.{{Cite book|last=Eguia Moraza|first=Iban|url=https://www.worldcat.org/oclc/1033544275|title=Rust high performance : learn to skyrocket the performance of your Rust applications|date=2018|isbn=978-1-78847-823-6|location=Birmingham, UK|oclc=1033544275}}

=Broadcasting=

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.{{Cite book|last1=Blandy|first1=Jim|last2=Orendoff|first2=Jason|last3=Tindall|first3=Leonara|url=https://books.google.com/books?id=qFkzEAAAQBAJ|title=Programming Rust|date=2019|isbn=9781492052548|location=Sebastopol}}

References

{{Reflist}}