-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Add exercise async1 #2382
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
Open
senekor
wants to merge
4
commits into
rust-lang:main
Choose a base branch
from
senekor:senekor/rvsyvlvuzyvu
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add exercise async1 #2382
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 @@ | ||
| # Async | ||
|
|
||
| Asynchronous programming is a model where tasks are delegated to a runtime that executes them concurrently. | ||
| It is particularly efficient for applications where many independent IO-operations are performed, e.g. web servers. | ||
|
|
||
| Rust provides the necessary primitives to do asynchronous programming in the language. | ||
| However, Rust's standard library does not include a runtime. | ||
| For these exercises, we will use the mainstream runtime called `tokio`. | ||
|
|
||
| ## Further information | ||
|
|
||
| - [Fundamentals of Asynchronous Programming](https://doc.rust-lang.org/book/ch17-00-async-await.html) | ||
| - [Tokio documentation](https://docs.rs/tokio/latest/tokio/) |
This file contains hidden or 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,42 @@ | ||
| // Alice is an elementary school teacher who needs to calculate the mean test | ||
| // score for three classes she teaches. Instead of calculating them one after | ||
| // the other, she decides to ask her friends Bob and Catherine for help. Working | ||
| // together, they can finish the job much faster. | ||
| // | ||
| // Let's simulate this using asynchronous programming. Each person is | ||
| // represented as an asynchronous task, which can be executed concurrently (i.e. | ||
| // they can be doing the calculations at the same time). | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "at the same time" is misleading |
||
|
|
||
| fn main() { | ||
| // Async tasks need to be executed by a "runtime", which is not provided by | ||
| // Rust's standard library. Here, we use the mainstream runtime `tokio`. | ||
| let rt = tokio::runtime::Builder::new_current_thread() | ||
| .build() | ||
| .unwrap(); | ||
|
|
||
| let scores_class_a = &[83, 77, 92]; | ||
| let scores_class_b = &[84, 88, 96]; | ||
| let scores_class_c = &[71, 83, 76]; | ||
|
|
||
| // TODO: Fix the compiler errors by making the spawned function async. | ||
| let alice = rt.spawn(calculate_mean_score(scores_class_a)); | ||
| let bob = rt.spawn(calculate_mean_score(scores_class_b)); | ||
| let catherine = rt.spawn(calculate_mean_score(scores_class_c)); | ||
|
|
||
| // Block the runtime on a task that awaits all three calculations. | ||
| let [mean_score_a, mean_score_b, mean_score_c]: [usize; _] = rt.block_on(async { | ||
| [ | ||
| // TODO: "await" all three tasks to fix the compiler error. | ||
| alice, bob, catherine, | ||
| ] | ||
| }); | ||
|
|
||
| assert_eq!(mean_score_a, 84); | ||
| assert_eq!(mean_score_b, 89); | ||
| assert_eq!(mean_score_c, 76); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The assertions can be moved to the body of |
||
| } | ||
|
|
||
| fn calculate_mean_score(score_list: &[usize]) -> usize { | ||
| let score_sum: usize = score_list.iter().sum(); | ||
| score_sum / score_list.len() | ||
| } | ||
This file contains hidden or 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 hidden or 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,42 @@ | ||
| // Alice is an elementary school teacher who needs to calculate the mean test | ||
| // score for three classes she teaches. Instead of calculating them one after | ||
| // the other, she decides to ask her friends Bob and Catherine for help. Working | ||
| // together, they can finish the job much faster. | ||
| // | ||
| // Let's simulate this using asynchronous programming. Each person is | ||
| // represented as an asynchronous task, which can be executed concurrently (i.e. | ||
| // they can be doing the calculations at the same time). | ||
|
|
||
| fn main() { | ||
| // Async tasks need to be executed by a "runtime", which is not provided by | ||
| // Rust's standard library. Here, we use the mainstream runtime `tokio`. | ||
| let rt = tokio::runtime::Builder::new_current_thread() | ||
| .build() | ||
| .unwrap(); | ||
|
|
||
| let scores_class_a = &[83, 77, 92]; | ||
| let scores_class_b = &[84, 88, 96]; | ||
| let scores_class_c = &[71, 83, 76]; | ||
|
|
||
| let alice = rt.spawn(calculate_mean_score(scores_class_a)); | ||
| let bob = rt.spawn(calculate_mean_score(scores_class_b)); | ||
| let catherine = rt.spawn(calculate_mean_score(scores_class_c)); | ||
|
|
||
| // Block the runtime on a task that awaits all three calculations. | ||
| let [mean_score_a, mean_score_b, mean_score_c]: [usize; _] = rt.block_on(async { | ||
| [ | ||
| alice.await.unwrap(), | ||
| bob.await.unwrap(), | ||
| catherine.await.unwrap(), | ||
| ] | ||
| }); | ||
|
|
||
| assert_eq!(mean_score_a, 84); | ||
| assert_eq!(mean_score_b, 89); | ||
| assert_eq!(mean_score_c, 76); | ||
| } | ||
|
|
||
| async fn calculate_mean_score(score_list: &[usize]) -> usize { | ||
| let score_sum: usize = score_list.iter().sum(); | ||
| score_sum / score_list.len() | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using a single thread without actual async work means that they won't finish much faster, in fact, they will be slightly slower.