Add exercise async1#2382
Conversation
b2f334e to
7f50737
Compare
7f50737 to
46533ad
Compare
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.
46533ad to
abc8969
Compare
mo8it
left a comment
There was a problem hiding this comment.
I like
- The requirement of adding
asyncand.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.
| publish = false | ||
|
|
||
| [dependencies] | ||
| tokio = { version = "1.52.1", features = ["rt"] } |
There was a problem hiding this comment.
version = "1" should be enough
| // 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 |
There was a problem hiding this comment.
Here, you say that they can be working at the same time. But in main, you create a single threaded runtime.
There was a problem hiding this comment.
"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.
|
What about using Something like that does actual work and is a valid usage for async. |
|
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. |
- 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.
|
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. |
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.