Skip to content

Commit e593187

Browse files
committed
Add async/await
Signed-off-by: Aisuko <[email protected]>
1 parent 2c75059 commit e593187

File tree

4 files changed

+1071
-1
lines changed

4 files changed

+1071
-1
lines changed

freesoftware/programming/rust/tokio.md

+53-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,30 @@
1-
# tokio
1+
# Crate tokio
22

33
In Rust, we call package as `Crate`. Crate tokio is a runtime for wrtiting reliable netwrok applications without compromising speed. And it is a event-driven, non-blocking I/O platform for writing asynchronous applications with Rust. It provides a rich set of components for building servers and clients.
44

55

6+
## Tokio provides a few major components:
7+
* A multi-thread runtime for executing components
8+
* An asynchronous version of the standard library
9+
* A large ecosystem of libraries
10+
11+
12+
## Tokio's role in your project
13+
14+
Asynchronous Ruse code does not run on it owns, so you must choose a runtime to execute it. The Tokio library is the most widely used runtime, surpassing all other runtimes in usage combined.
15+
16+
And Tokio provides the asyncchronous versions of the standard library.
17+
18+
19+
## When not to use Tokio
20+
21+
* Tojio is designed for IO-bound applications where each individual task spends most of its time waiting got IO. If the only thing your application does is run computations in parallel, you should be using rayon.
22+
23+
* Reading a lot of files.
24+
25+
* Sending a single web request.
26+
27+
628
## Working with Tasks
729

830
Asynchronous program in Rust are based around lightweigh, non-blocking units of execution called tasks.
@@ -26,3 +48,33 @@ Tokio is able to concurrently run many tasks on a few thread by repeatedly swapp
2648
The core threads are where all asynchronous code runs, and Tokio will by default spawn one for each CPU core. You can use the env variable `TOKIO_WORKER_THREADS` to override the default value.
2749

2850
The blocking threads are spawned on demand, can be used to run blocking code that would otherwise block other tasks from running and are kept alive when not used for a certain amount of time which can be configured with `thread_keep_alive`. Since it is not possible for Tokio to swap out blocking tasks, like it can do with asynchronous code, the upper limit on the number of blocking threads is very large. These limits can be configured on the Builder.
51+
52+
53+
## What is asynchronous programming?
54+
55+
Most computer programs are executed in the same order in which they are written. The first line executes, then the next, and so on. With synchronous programming, when a program encounters an operation that cannot be completed immediately, it will block until the operation completes. For example, establishing a TCP connection requires an exchange with a peer over the network, which can take a sizeable amount of time. During this time, the thread is blocked.
56+
57+
With asynchronous programming, operations that cannot complete immediately are suspended to the background. The thread is not blocked, and can continue running other things. Once the operation completes, the task is unsuspended and continues processing from where it left off.
58+
59+
Although asynchronous programming can result in faster applications, it often results in much more complicated programs. The programmer is required to track all the state necessary to resume work once the asynchronous operation completes. Historically, this is a tedious and error-prone task.
60+
61+
62+
## Compile-time green-threading
63+
64+
Rust implements asynchronous programing using a feature called `async/await`. Functions that perform asynchronous operations are labeled with the `async` keyword. For example, the connect function is defined like this:
65+
66+
```
67+
use mini_redis::Result;
68+
use mini_redis::client::Client;
69+
use tokio::net::ToSocketAddrs;
70+
71+
pub async fn connect<T: ToSocketAddrs>(addr: T) -> Result<Client> {
72+
// ...
73+
}
74+
```
75+
76+
The `async fn` definition looks like a regular synchronous function, but operate asynchronously. Rust transforms the `async fn` at compile time into a routine that operates asynchronously. Any calls to `.await` within the `async fn` yield control bakc to the thread. The thread may do other work while the operation processes in the background.
77+
78+
79+
# Reference
80+
* [Tokio](https://tokio.rs/)

0 commit comments

Comments
 (0)