Tokio (software)

Last updated
Tokio
Original author(s) Carl Lerche
Initial releaseDecember 23, 2020;2 years ago (2020-12-23)
Stable release
1.28.0 [1]   OOjs UI icon edit-ltr-progressive.svg
Repository
Written in Rust
Operating system macOS, Windows, Linux, FreeBSD, WebAssembly
Type Asynchronous runtime
License MIT License
Website 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. [2] [3] [4]

Contents

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

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

For example:

#[tokio::main]asyncfnmain()-> Result<()>{leturl="https://en.wikipedia.org/";lettext=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]

Compile-time green-threading

Tokio further allows users to create tasks, which are green threads, using a tokio::spawn() function. Green threads run at the user level, providing parallelism when native threads are not always available. [22] Previous versions of Rust implemented green threading; this functionality was removed in Rust 1.0. [23] Unlike futures, tasks do not need to use .await, as the task will be automatically executed when a thread is available. [18]

Socket listening

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

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

Related Research Articles

<span class="mw-page-title-main">F Sharp (programming language)</span> Microsoft programming language

F# is a functional-first, general-purpose, strongly typed, multi-paradigm programming language that encompasses functional, imperative, and object-oriented programming methods. It is most often used as a cross-platform Common Language Infrastructure (CLI) language on .NET, but can also generate JavaScript and graphics processing unit (GPU) code.

Coroutines are computer program components that allow execution to be suspended and resumed, generalizing subroutines for cooperative multitasking. Coroutines are well-suited for implementing familiar program components such as cooperative tasks, exceptions, event loops, iterators, infinite lists and pipes.

In computing, a stack trace is a report of the active stack frames at a certain point in time during the execution of a program. When a program is run, memory is often dynamically allocated in two places; the stack and the heap. Memory is continuously allocated on a stack but not on a heap, thus reflective of their names. Stack also refers to a programming construct, thus to differentiate it, this stack is referred to as the program's function call stack. Technically, once a block of memory has been allocated on the stack, it cannot be easily removed as there can be other blocks of memory that were allocated before it. Each time a function is called in a program, a block of memory called an activation record is allocated on top of the call stack. Generally, the activation record stores the function's arguments and local variables. What exactly it contains and how it's laid out is determined by the calling convention.

In computer programming, an entry point is the place in a program where the execution of a program begins, and where the program has access to command line arguments.

The actor model in computer science is a mathematical model of concurrent computation that treats an actor as the basic building block of concurrent computation. In response to a message it receives, an actor can: make local decisions, create more actors, send more messages, and determine how to respond to the next message received. Actors may modify their own private state, but can only affect each other indirectly through messaging.

In computer science, asynchronous I/O is a form of input/output processing that permits other processing to continue before the transmission has finished. A name used for asynchronous I/O in the Windows API is overlapped I/O.

In computer science, future, promise, delay, and deferred refer to constructs used for synchronizing program execution in some concurrent programming languages. They describe an object that acts as a proxy for a result that is initially unknown, usually because the computation of its value is not yet complete.

Concurrent computing is a form of computing in which several computations are executed concurrently—during overlapping time periods—instead of sequentially—with one completing before the next starts.

Cooperative multitasking, also known as non-preemptive multitasking, is a style of computer multitasking in which the operating system never initiates a context switch from a running process to another process. Instead, in order to run multiple applications concurrently, processes voluntarily yield control periodically or when idle or logically blocked. This type of multitasking is called cooperative because all programs must cooperate for the scheduling scheme to work.

In computer programming, a green thread is a thread that is scheduled by a runtime library or virtual machine (VM) instead of natively by the underlying operating system (OS). Green threads emulate multithreaded environments without relying on any native OS abilities, and they are managed in user space instead of kernel space, enabling them to work in environments that do not have native thread support.

Svelte is a free and open-source front end component framework or language created by Rich Harris and maintained by the Svelte core team members. Svelte is not a monolithic JavaScript library imported by applications: instead, Svelte compiles HTML templates to specialized code that manipulates the DOM directly, which may reduce the size of transferred files and give better client performance. Application code is also processed by the compiler, inserting calls to automatically recompute data and re-render UI elements when the data they depend on is modified. This also avoids the overhead associated with runtime intermediate representations, such as virtual DOM, unlike traditional frameworks which carry out the bulk of their work at runtime, i.e. in the browser.

CommonJS is a project with the goal to establish conventions on the module ecosystem for JavaScript outside of the web browser. The primary reason for its creation was a major lack of commonly accepted forms of JavaScript module units which could be reusable in environments different from that provided by conventional web browsers running JavaScript scripts.

Windows Runtime (WinRT) is a platform-agnostic component and application architecture first introduced in Windows 8 and Windows Server 2012 in 2012. It is implemented in C++ and officially supports development in C++, Rust/WinRT, Python/WinRT, JavaScript-TypeScript, and the managed code languages C# and Visual Basic .NET (VB.NET).

Elixir is a functional, concurrent, high-level general-purpose programming language that runs on the BEAM virtual machine, which is also used to implement the Erlang programming language. Elixir builds on top of Erlang and shares the same abstractions for building distributed, fault-tolerant applications. Elixir also provides tooling and an extensible design. The latter is supported by compile-time metaprogramming with macros and polymorphism via protocols.

In computer programming, the async/await pattern is a syntactic feature of many programming languages that allows an asynchronous, non-blocking function to be structured in a way similar to an ordinary synchronous function. It is semantically related to the concept of a coroutine and is often implemented using similar techniques, and is primarily intended to provide opportunities for the program to execute other code while waiting for a long-running, asynchronous task to complete, usually represented by promises or similar data structures. The feature is found in C# 5.0, C++20, Python 3.5, F#, Hack, Julia, Dart, Kotlin 1.1, Rust 1.39, Nim 0.9.4, JavaScript ES2017, Swift 5.5 and Zig, with some experimental work in extensions, beta versions, and particular implementations of Scala.

<span class="mw-page-title-main">WebAssembly</span> Cross-platform assembly language and bytecode designed for execution in web browsers

WebAssembly defines a portable binary-code format and a corresponding text format for executable programs as well as software interfaces for facilitating interactions between such programs and their host environment.

<span class="mw-page-title-main">Deno (software)</span> Secure Javascript and Typescript runtime

Deno is a runtime for JavaScript, TypeScript, and WebAssembly that is based on the V8 JavaScript engine and the Rust programming language. Deno was co-created by Ryan Dahl, who also created Node.js.

<span class="mw-page-title-main">Thing Description</span>

The Thing Description (TD) (or W3C WoT Thing Description (TD)) is a royalty-free, open information model with a JSON based representation format for the Internet of Things (IoT). A TD provides a unified way to describe the capabilities of an IoT device or service with its offered data model and functions, protocol usage, and further metadata. Using Thing Descriptions help reduce the complexity of integrating IoT devices and their capabilities into IoT applications.

io_uring is a Linux kernel system call interface for storage device asynchronous I/O operations addressing performance issues with similar interfaces provided by functions like read /write or aio_read /aio_write etc. for operations on data accessed by file descriptors.

Signal Transition Graphs (STGs) are typically used in electronic engineering and computer engineering to describe dynamic behaviour of asynchronous circuits, for the purposes of their analysis or synthesis.

References

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