Skip to content

Commit 6957e04

Browse files
Added Spawning example
1 parent e224ead commit 6957e04

File tree

5 files changed

+61
-1
lines changed

5 files changed

+61
-1
lines changed

examples/06_04_spawning/Cargo.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
name = "example_06_04_spawning"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]
9+
futures = "0.3"
10+
11+
[dependencies.async-std]
12+
version = "1.12.0"
13+
features = ["attributes"]

examples/06_04_spawning/src/lib.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#![cfg(test)]
2+
#![allow(dead_code)]
3+
4+
// ANCHOR: example
5+
use async_std::{task, net::TcpListener, net::TcpStream};
6+
use futures::AsyncWriteExt;
7+
8+
async fn process_request(stream: &mut TcpStream) -> Result<(), std::io::Error>{
9+
stream.write_all(b"HTTP/1.1 200 OK\r\n\r\n").await?;
10+
stream.write_all(b"Hello World").await?;
11+
Ok(())
12+
}
13+
14+
async fn main() {
15+
let listener = TcpListener::bind("127.0.0.1:8080").await.unwrap();
16+
loop {
17+
// Accept a new connection
18+
let (mut stream, _) = listener.accept().await.unwrap();
19+
// Now process this request without blocking the main loop
20+
task::spawn(async move {process_request(&mut stream).await});
21+
}
22+
}
23+
// ANCHOR_END: example
24+
25+
#[cfg(test)]
26+
mod tests {
27+
use super::*;
28+
29+
#[async_std::test]
30+
async fn run_example() {
31+
main().await
32+
}
33+
}

examples/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ members = [
1010
"05_02_iteration_and_concurrency",
1111
"06_02_join",
1212
"06_03_select",
13+
"06_04_spawning",
1314
"07_05_recursion",
1415
"09_01_sync_tcp_server",
1516
"09_02_async_tcp_server",
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# `Spawning`
2+
3+
Spawning allows you to run a new asynchronous task in the background. This allows us to continue executing other code
4+
while it is running.
5+
6+
Say we have a webserver that wants to accept connections without blocking the main thread.
7+
We can do this by using the `async_std::task::spawn` function to spawn a new task that runs
8+
the connection handler. This function takes a future and returns a `JoinHandle` that can be
9+
used to await the result of the spawned task.
10+
11+
```rust,edition2018
12+
{{#include ../../examples/06_04_spawning/src/lib.rs:example}}
13+
```

src/SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
- [Executing Multiple Futures at a Time](06_multiple_futures/01_chapter.md)
1717
- [`join!`](06_multiple_futures/02_join.md)
1818
- [`select!`](06_multiple_futures/03_select.md)
19-
- [TODO: Spawning]()
19+
- [Spawning](06_multiple_futures/04_spawning.md)
2020
- [TODO: Cancellation and Timeouts]()
2121
- [TODO: `FuturesUnordered`]()
2222
- [Workarounds to Know and Love](07_workarounds/01_chapter.md)

0 commit comments

Comments
 (0)