-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
88 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "example_06_04_spawning" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
futures = "0.3" | ||
|
||
[dependencies.async-std] | ||
version = "1.12.0" | ||
features = ["attributes"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#![cfg(test)] | ||
#![allow(dead_code)] | ||
|
||
// ANCHOR: example | ||
use async_std::{task, net::TcpListener, net::TcpStream}; | ||
use futures::AsyncWriteExt; | ||
|
||
async fn process_request(stream: &mut TcpStream) -> Result<(), std::io::Error>{ | ||
stream.write_all(b"HTTP/1.1 200 OK\r\n\r\n").await?; | ||
stream.write_all(b"Hello World").await?; | ||
Ok(()) | ||
} | ||
|
||
async fn main() { | ||
let listener = TcpListener::bind("127.0.0.1:8080").await.unwrap(); | ||
loop { | ||
// Accept a new connection | ||
let (mut stream, _) = listener.accept().await.unwrap(); | ||
// Now process this request without blocking the main loop | ||
task::spawn(async move {process_request(&mut stream).await}); | ||
} | ||
} | ||
// ANCHOR_END: example | ||
use std::time::Duration; | ||
async fn my_task(time: Duration) { | ||
println!("Hello from my_task with time {:?}", time); | ||
task::sleep(time).await; | ||
println!("Goodbye from my_task with time {:?}", time); | ||
} | ||
// ANCHOR: join_all | ||
use futures::future::join_all; | ||
async fn task_spawner(){ | ||
let tasks = vec![ | ||
task::spawn(my_task(Duration::from_secs(1))), | ||
task::spawn(my_task(Duration::from_secs(2))), | ||
task::spawn(my_task(Duration::from_secs(3))), | ||
]; | ||
// If we do not await these tasks and the function finishes, they will be dropped | ||
join_all(tasks).await; | ||
} | ||
// ANCHOR_END: join_all | ||
|
||
#[test] | ||
fn run_task_spawner() { | ||
futures::executor::block_on(task_spawner()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# `Spawning` | ||
|
||
Spawning allows you to run a new asynchronous task in the background. This allows us to continue executing other code | ||
while it runs. | ||
|
||
Say we have a web server that wants to accept connections without blocking the main thread. | ||
To achieve this, we can use the `async_std::task::spawn` function to create and run a new task that handles the | ||
connections. This function takes a future and returns a `JoinHandle`, which can be used to wait for the result of the | ||
task once it's completed. | ||
|
||
```rust,edition2018 | ||
{{#include ../../examples/06_04_spawning/src/lib.rs:example}} | ||
``` | ||
|
||
The `JoinHandle` returned by `spawn` implements the `Future` trait, so we can `.await` it to get the result of the task. | ||
This will block the current task until the spawned task completes. If the task is not awaited, your program will | ||
continue executing without waiting for the task, cancelling it if the function is completed before the task is finished. | ||
|
||
```rust,edition2018 | ||
{{#include ../../examples/06_04_spawning/src/lib.rs:join_all}} | ||
``` | ||
|
||
To communicate between the main task and the spawned task, we can use channels | ||
provided by the async runtime used. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters