Tokio (software) explained

Tokio
Tokio
Logo Size:200px
Author:Carl Lerche
Programming Language:Rust
Operating System:macOS, Windows, Linux, FreeBSD, WebAssembly
Genre:Asynchronous runtime
License:MIT License

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.[1] [2] [3]

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 tokio-uring.[4] [5] [6] The name "Tokio" is derived from Tokyo and mio.[7] The preliminary version of Tokio was released in January 2017,[8] followed by a full release in December 2020.[9] [10] In 2017, Tokio received a grant from the Mozilla Open Source Support fund.[11] In April 2021, Tokio funded its first paid contributor, Alice Ryhl, for her work both developing the project and assisting its users.[12] [13]

While Rust has supported asynchronous functions since version 1.39, released in November 2019,[14] it provides no facilities to execute them, requiring an external runtime for that purpose.[15] 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.[16] 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.[17]

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.[18]

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.[17]

For example:

  1. [tokio::main]

async fn main -> Result<>

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.[19] In addition, Tokio supports io_uring, a Linux asynchronous I/O syscall interface, in a separate crate named tokio-uring.[20]

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.[21]

Socket listening

Tokio is capable of listening on a socket through a non-blocking approach.[4] In particular, the TcpListener structure binds a Transmission Control Protocol (TCP) socket listener to an address and asynchronously executes function.[22]

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.[23]

External links

Notes and References

  1. Book: Chanda, Abhishek. Network Programming with Rust: Build fast and resilient network servers and clients by leveraging Rust's memory-safety and concurrency features.. 2018. Packt Publishing. 978-1-78862-171-7. Birmingham. 1028194311.
  2. Book: Sharma, Rahul. Mastering Rust : learn about memory safety, type system, concurrency, and the new features of Rust 2018 edition. 2019. Vesa Kaihlavirta. 978-1-78934-118-8. Second. Birmingham, UK. 1090681119.
  3. Web site: De Simone. Sergio. 2021-01-06. Rust Asynchronous Runtime Tokio Reaches 1.0. 2021-11-21. InfoQ. en.
  4. Web site: Announcing Tokio . August 3, 2016 . Lerche . Carl . December 11, 2022.
  5. Web site: Finagle: A Protocol-Agnostic RPC System . August 19, 2011 . December 11, 2022.
  6. Book: Gomez. Guillaume. Boucher. Antoni. Rust Programming By Example: Enter the World of Rust by Building Engaging, Concurrent, Reactive, and Robust Applications. 2018. Packt Publishing. 9781788470308. Birmingham.
  7. Web site: 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. . August 3, 2016 . Lerche . Carl . . December 11, 2022.
  8. Web site: Announcing Tokio 0.1 . Lerche . Carl . Crichton . Alex . Turon . Aaron . December 11, 2022.
  9. Web site: Krill. Paul. 2021-01-08. Tokio Rust runtime reaches 1.0 status. 2021-09-03. InfoWorld. en.
  10. Web site: Announcing Tokio 1.0 . Lerche . Carl . December 11, 2022.
  11. Web site: Mozilla Awards $365,000 to Open Source Projects as part of MOSS. 2021-11-21. LWN.net.
  12. Web site: Welcoming Alice Ryhl as the first paid Tokio contributor. 2021-11-28. Tokio.
  13. Tokio Ecosystem with Alice Ryhl. Rustacean Station. Allen Wyma. 12 November 2021. 2021-11-26.
  14. Web site: Rust Gets Zero-Cost Async/Await Support in Rust 1.39. 2021-11-28. InfoQ. en.
  15. Web site: The Async Ecosystem. 2021-11-28. Asynchronous Programming in Rust.
  16. Web site: Matsakis. Niko. 2019-11-07. Async-await on stable Rust!. 2021-11-28. Rust Blog. en.
  17. Web site: Hello Tokio. 2021-11-28. Tokio.
  18. Book: Rappl Moraza, Florian. Modern Frontend Development with Node.js: A Compendium for Modern JavaScript Web Development Within the Node.js Ecosystem. 2022. 9781804617380. Birmingham, UK.
  19. Web site: I/O. December 11, 2022. Tokio.
  20. Web site: Announcing tokio-uring: io-uring support for Tokio. 2021-11-28. Tokio.
  21. Web site: Four for the ages . April 13, 2001 . Sintes . Tony . . January 5, 2023.
  22. Book: Eguia Moraza, Iban. Rust high performance : learn to skyrocket the performance of your Rust applications. 2018. 978-1-78847-823-6. Birmingham, UK. 1033544275.
  23. Book: Blandy. Jim. Orendoff. Jason. Tindall. Leonara. Programming Rust. 2019. 9781492052548. Sebastopol.