Skip to content

Add exercise async1#2382

Open
senekor wants to merge 4 commits intorust-lang:mainfrom
senekor:senekor/rvsyvlvuzyvu
Open

Add exercise async1#2382
senekor wants to merge 4 commits intorust-lang:mainfrom
senekor:senekor/rvsyvlvuzyvu

Conversation

@senekor
Copy link
Copy Markdown
Contributor

@senekor senekor commented Apr 18, 2026

The goal here was to get the first bit of "muscle memory" for using the async and await keywords. The little story should make it more intuitive for users why asynchronous programming is needed in the first place.

This exercise can be moved to the location corresponding to the book in a later PR, to keep the diff of this one clean.

@senekor senekor force-pushed the senekor/rvsyvlvuzyvu branch 2 times, most recently from b2f334e to 7f50737 Compare April 18, 2026 05:06
@senekor senekor force-pushed the senekor/rvsyvlvuzyvu branch from 7f50737 to 46533ad Compare April 18, 2026 05:20
The goal here was to get the first bit of "muscle memory" for using the
async and await keywords. The little story should make it more intuitive
for users why asynchronous programming is needed in the first place.

This exercise will be moved to the location corresponding to the book in
a later commit, to keep the diff of this one clean.
@senekor senekor force-pushed the senekor/rvsyvlvuzyvu branch from 46533ad to abc8969 Compare April 18, 2026 21:31
Copy link
Copy Markdown
Contributor

@mo8it mo8it left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like

  • The requirement of adding async and .await
  • Spawning tasks, awaiting them and then checking that they are done
  • The strory about splitting tasks among different workers

I don't like

  • The usage of atomics
  • The work done in each task. Printing locks under the hood so the tasks will run mostly sequentially even if the runtime was multi-threaded
  • The story details about the boys and soccer. Some might find it a bit childish for tasks meant mainly for adults

What about letting the tasks do some calculation and return the result? Then all three results could be checked. This way, we don't need atomics or printing.

Comment thread dev/Cargo.toml Outdated
Comment thread exercises/24_async/async1.rs Outdated
Comment thread exercises/24_async/async1.rs Outdated
Comment thread exercises/24_async/async1.rs Outdated
// finish the chores earlier and have more time left to play soccer.
//
// Let's simulate this using asynchronous programming. Each boy is represented
// as an asynchronous task, which can be executed concurrently (they can be
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, you say that they can be working at the same time. But in main, you create a single threaded runtime.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"concurrently" is the more precise word. I think it's good for teaching to use a single-threaded runtime, to show that async can make a difference even without multi-threading. Though, as you mentioned, just printing to the console might not be best for that.

Comment thread exercises/24_async/async1.rs Outdated
@mo8it
Copy link
Copy Markdown
Contributor

mo8it commented Apr 25, 2026

What about using tokio::fs to open three files asynchronously, reading their content by parsing each line as a number and summing them up?

Something like that does actual work and is a valid usage for async.

@mo8it
Copy link
Copy Markdown
Contributor

mo8it commented Apr 25, 2026

The story could be something like teachers want to calculate the mean grade for three different classes. Instead of only one teacher doing all the work or doing it sequentially, they can do it async.

senekor added 3 commits April 25, 2026 15:33
- Remove confusing use of atomics. Use return values of async tasks
  instead, to ensure all tasks are awaited.
- Remove use of `println!()`, which uses a global lock and cannot be
  executed in parallel.
@senekor
Copy link
Copy Markdown
Contributor Author

senekor commented Apr 25, 2026

Agree with most of these points. I redid the exercise to hopefully address them. I'm not sure yet about using a multi-threaded runtime and using file IO. Yes, it would be more realistic, but I'm concerned it might be more complicated and distract from the intended lesson here - building a little async/await muscle memory.

Copy link
Copy Markdown
Contributor

@mo8it mo8it left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the overall direction, but I think that trying to keep the exercise simple results in misleading the users. By avoiding async work, they won't get the difference to multi-threading and will be confused because the solution is slower on a single thread.

CHORES_DONE.fetch_add(1, Ordering::SeqCst);
assert_eq!(mean_score_a, 84);
assert_eq!(mean_score_b, 89);
assert_eq!(mean_score_c, 76);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The assertions can be moved to the body of block_on to avoid the indirection by the array.

// 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.
Copy link
Copy Markdown
Contributor

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.

static CHORES_DONE: AtomicU8 = AtomicU8::new(0);
// 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).
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"at the same time" is misleading

@mo8it
Copy link
Copy Markdown
Contributor

mo8it commented May 9, 2026

Maybe somthing like this?

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 alice = rt.spawn(calculate_mean_score("a.txt"));
    let bob = rt.spawn(calculate_mean_score("b.txt"));
    let catherine = rt.spawn(calculate_mean_score("c.txt"));

    // Block the runtime on a task that awaits all three calculations.
    rt.block_on(async {
        // TODO: "await" all three tasks.
        assert_eq!(alice, 84);
        assert_eq!(bob, 89);
        assert_eq!(catherine, 76);
    });
}

// TODO: Fix the compiler errors by making the spawned function async.
fn calculate_mean_score(scores_file: &str) -> usize {
    // Read the file asynchronously
    let file = tokio::fs::read_to_string(scores_file).await.unwrap();

    // Initialize the sum and the number of scores
    let mut sum = 0;
    let mut n = 0;
    for line in file.lines() {
        // Parse every line as a score
        let score = line.parse::<usize>().unwrap();
        sum += score;
        n += 1;
    }

    sum / n
}

It is not much more complicated. Remember that this exercise is one towards the end, so the users aren't complete beginners at this point.

If we take this direction, we should add the IO tasks before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants