HOME

TheInfoList



OR:

Tokio is a
software library In computing, a library is a collection of resources that can be leveraged during software development to implement a computer program. Commonly, a library consists of executable code such as compiled functions and classes, or a library can ...
for the
Rust Rust is an iron oxide, a usually reddish-brown oxide formed by the reaction of iron and oxygen in the catalytic presence of water or air moisture. Rust consists of hydrous iron(III) oxides (Fe2O3·nH2O) and iron(III) oxide-hydroxide (FeO(OH) ...
programming language. It provides a runtime and functions that enable the use of
asynchronous I/O In computer science, asynchronous I/O (also non-sequential I/O) is a form of input/output processing that permits other processing to continue before the I/O operation has finished. A name used for asynchronous I/O in the Windows API is '' over ...
, allowing for concurrency in regards to task completion. Tokio was released in August 2016 for Rust, a
general-purpose programming language In computer software, a general-purpose programming language (GPL) is a programming language for building software in a wide variety of application Domain (software engineering), domains. Conversely, a Domain-specific language, domain-specific pro ...
. Developed by Carl Lerche, Tokio began as a network application framework and supports features such as socket listening and
broadcasting Broadcasting is the data distribution, distribution of sound, audio audiovisual content to dispersed audiences via a electronic medium (communication), mass communications medium, typically one using the electromagnetic spectrum (radio waves), ...
, 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 Futures may mean: Finance *Futures contract, a tradable financial derivatives contract *Futures exchange, a financial market where futures contracts are traded *''Modern Trader'', formerly Futures, an American finance magazine Music * ''Futures' ...
, allowing for network-based
middleware Middleware is a type of computer software program that provides services to software applications beyond those available from the operating system. It can be described as "software glue". Middleware makes it easier for software developers to imple ...
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 In distributed computing, a remote procedure call (RPC) is when a computer program causes a procedure (subroutine) to execute in a different address space (commonly on another computer on a shared computer network), which is written as if it were a ...
(RPC) system developed at Twitter for
Java virtual machine A Java virtual machine (JVM) is a virtual machine that enables a computer to run Java programs as well as programs written in other languages that are also compiled to Java bytecode. The JVM is detailed by a specification that formally descr ...
s (JVM), allowing distributed systems to communicate within a JVM. Tokio utilizes the lower-level Rust crate mio, itself using system calls such as
epoll epoll is a Linux kernel system call for a scalable I/O event notification mechanism, first introduced in version 2.5.45 of the Linux kernel. Its function is to monitor multiple file descriptors to see whether I/O is possible on any of them. It is ...
(
Linux Linux ( ) is a family of open source Unix-like operating systems based on the Linux kernel, an kernel (operating system), operating system kernel first released on September 17, 1991, by Linus Torvalds. Linux is typically package manager, pac ...
),
kqueue Kqueue is a scalable event notification interface introduced in FreeBSD 4.1 in July 2000, also supported in NetBSD, OpenBSD, DragonFly BSD, and macOS. Kqueue was originally authored in 2000 by Jonathan Lemon, then involved with the FreeBSD Core T ...
(
FreeBSD FreeBSD is a free-software Unix-like operating system descended from the Berkeley Software Distribution (BSD). The first version was released in 1993 developed from 386BSD, one of the first fully functional and free Unix clones on affordable ...
), and the
input/output completion port Input/output completion port (IOCP) is an API for performing multiple simultaneous asynchronous input/output operations in Windows NT versions 3.5 and later, AIX and on Solaris 10 and later. An input/output completion port object is created and ...
(IOCP) API (
Windows Windows is a Product lining, product line of Proprietary software, proprietary graphical user interface, graphical operating systems developed and marketed by Microsoft. It is grouped into families and subfamilies that cater to particular sec ...
). For Linux it can also use io_uring vi
tokio-uring
The name "Tokio" is derived from
Tokyo Tokyo, officially the Tokyo Metropolis, is the capital of Japan, capital and List of cities in Japan, most populous city in Japan. With a population of over 14 million in the city proper in 2023, it is List of largest cities, one of the most ...
and mio. The preliminary version of Tokio was released in January 2017, followed by a full release in December 2020. In 2017, Tokio received a grant from the Mozilla Open Source Support fund. In April 2021, Tokio funded its first paid contributor, Alice Ryhl, for her work both developing the project and assisting its users. While Rust has supported asynchronous functions since version 1.39, released in November 2019, it provides no facilities to execute them, requiring an external runtime for that purpose. 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. 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. Users of Tokio include the development teams behind
Discord Discord is an instant messaging and Voice over IP, VoIP social platform which allows communication through Voice over IP, voice calls, Videotelephony, video calls, text messaging, and digital media, media. Communication can be private or take ...
and AWS Lambda. The
JavaScript JavaScript (), often abbreviated as JS, is a programming language and core technology of the World Wide Web, alongside HTML and CSS. Ninety-nine percent of websites use JavaScript on the client side for webpage behavior. Web browsers have ...
and
TypeScript TypeScript (abbreviated as TS) is a high-level programming language that adds static typing with optional type annotations to JavaScript. It is designed for developing large applications and transpiles to JavaScript. It is developed by Micr ...
runtime Deno uses Tokio under the hood, in comparison to the JavaScript runtime Node.js, which uses the libuv library.


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 # okio::main/code> macro. For example: # okio::mainasync fn main() -> Result<()> In this example, the reqwest crate is used to request the
HyperText Markup Language Hypertext Markup Language (HTML) is the standard markup language for documents designed to be displayed in a web browser. It defines the content and structure of web content. It is often assisted by technologies such as Cascading Style Sheet ...
(HTML) for
English Wikipedia The English Wikipedia is the primary English-language edition of Wikipedia, an online encyclopedia. It was created by Jimmy Wales and Larry Sanger on 15 January 2001, as Wikipedia's first edition. English Wikipedia is hosted alongside o ...
. 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(). In addition, Tokio supports io_uring, a Linux asynchronous I/O syscall interface, in a separate crate named tokio-uring.


The coroutine model

Tokio further allows users to create tasks, which are stackless
coroutines 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, ...
, using a tokio::spawn() function. Tasks run at the user level, providing concurrency even when there is only a single thread.


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 The Transmission Control Protocol (TCP) is one of the main communications protocol, protocols of the Internet protocol suite. It originated in the initial network implementation in which it complemented the Internet Protocol (IP). Therefore, th ...
(TCP) socket listener to an address and asynchronously executes function.


Broadcasting

Tokio provides a
broadcast Broadcasting is the data distribution, distribution of sound, audio audiovisual content to dispersed audiences via a electronic medium (communication), mass communications medium, typically one using the electromagnetic spectrum (radio waves), ...
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.


References


External links

* * {{GitHub, tokio-rs/tokio, Tokio
Tokio
on crates.io 2020 software Free software programmed in Rust Free computer libraries Software using the MIT license