Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async fixes #546

Merged
merged 1 commit into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@
- [Pitfalls](async/pitfalls.md)
- [Blocking the Executor](async/pitfalls/blocking-executor.md)
- [Pin](async/pitfalls/pin.md)
- [Async Traits](async/pitfalls/pin.md)
- [Async Traits](async/pitfalls/async-traits.md)
- [Exercises](exercises/day-4/async.md)

# Final Words
Expand Down
4 changes: 2 additions & 2 deletions src/async.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ limited number of threads. This is because the per-task overhead is typically
very low and operating systems provide primitives for efficiently identifying
I/O that is able to proceed.

Rust's asynchronous operation is based around "futures", which represent work
that may be completed in the future. Futures are "polled" until they signal that
Rust's asynchronous operation is based on "futures", which represent work that
may be completed in the future. Futures are "polled" until they signal that
they are complete.

Futures are polled by an async runtime, and several different runtimes are
Expand Down
17 changes: 13 additions & 4 deletions src/async/channels.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ async fn ping_handler(mut input: Receiver<()>) {
async fn main() {
let (sender, receiver) = mpsc::channel(32);
let ping_handler_task = tokio::spawn(ping_handler(receiver));
for _ in 0..10 {
for i in 0..10 {
sender.send(()).await.expect("Failed to send ping.");
println!("Sent {} pings so far.", i + 1);
}

std::mem::drop(sender);
Expand All @@ -29,8 +30,16 @@ async fn main() {

<details>

- Overall, the interface is similar to the `sync` channels as seen in the [morning class](concurrency/channels.md).
- The `Flume` crate has channels that implement both `sync` and `async` `send` and `recv`. This can be convenient for complex application with both IO and heavy CPU processing tasks.
- What makes working with `async` channels preferable is the ability to combine them with other `future`s to combine them and create complex control flow.
* Change the channel size to `3` and see how it affects the execution.

* Overall, the interface is similar to the `sync` channels as seen in the
[morning class](concurrency/channels.md).

* The `Flume` crate has channels that implement both `sync` and `async` `send`
and `recv`. This can be convenient for complex application with both IO and
heavy CPU processing tasks.

* What makes working with `async` channels preferable is the ability to combine
them with other `future`s to combine them and create complex control flow.

</details>
3 changes: 2 additions & 1 deletion src/async/control-flow.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Futures Control Flow

Futures can be combined together to produce concurrent compute flow graphs. We will cover multiple common operations:
Futures can be combined together to produce concurrent compute flow graphs. We
will cover multiple common operations:

----

Expand Down
10 changes: 5 additions & 5 deletions src/async/futures.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ async fn count_to(count: i32) -> i32 {

#[tokio::main]
async fn main() {
let _: () = count_to(13);
println!("Final count is: {}!", count_to(13).await);

// Uncomment the following line to see the return type of the async call.
// let _: () = count_to(13);

}
```

Expand Down Expand Up @@ -49,10 +53,6 @@ block to pause until that Future is ready, and then evaluates to its output.

<details>

* Run the example and look at the error message. `_: () = ..` is a common
technique for getting the type of an expression. Try adding a `.await` in
`main`.

* The `Future` and `Poll` types are conceptually quite simple, and implemented as
such in `std::task`.

Expand Down