Javaru

Javaru

Kotlin Coroutine Confidence: Add `parallel/v9` (pg 125)

When I ran the parallel/v8 example on page 125, I did not get the results described of not seeing the timer’s output until the program almost exits. I got the timer output right from the start, and every second thereafter. After a few minutes of trying to figure out why, I gave up and kept reading. Once I got to page 126, I saw why. It was because the slowHash() function was already a suspend function with the yield() in it. This is shown as a change to the code on page 126, but it was not a change since it was already present in the downloaded code, and therefore present for the run on page 125. Thus the different result.

Since I am running each example as soon as I first encounter it, (and up to this point it has always worked doing that :slight_smile:) this is unexpected behavior. As I said, I spent a few minutes trying to figure things out. I didn’t even look a the slowHash() function since it wasn’t a point of discussion. (And I’m not sure I would have noticed the issue had I looked at the function since that function is not discussed in v8 until it is modified on page 126.)

As a suggestion, I think the changes to the slowHash() function on page 126 should be broken out to a parallel/v9 instance. And the parallel/v8 slowHash() function should not be a suspend function or have the yield(). That way the “bad” result occurs when the code is run on page 125. Another option is to just not include the suspend and yield() in the parallel/v8 version and have the user make the change in their code when they read page 126. But that may be less desirable since until now, there have not been any code changes needed by the user, and it breaks the “incremental versions” paradigm in use. (And it may prevent you from testing things properly),

Assuming you go with a v9, then the sentence:

Run either of the two previous programs again with this upgraded slowHash()
function, and everything works the way we wanted. Our extra timer coroutine can print its output every one second, and our timeout code can cancel the work promptly.

can change to something like:

You can also run the previous timeout program with this upgraded slowHash()
function, and everything will work the way we wanted. Our timeout code can cancel the work promptly.

book-kotlin-coroutine-confidence version B3

Marked As Solved

sam-cooper

sam-cooper

Author of Kotlin Coroutine Confidence

I hadn’t considered this! Thanks for pointing it out. I guess I was trying to be clever by combining two steps into a single version of the code. I can see how that would cause a problem, since the code in the download no longer behaves the way the book is describing.

I think adding a v9 is a good solution; thanks for the suggestion.

Thank you for pointing out that the sentence after the code example could be a problem too, since it refers to programs from two different versions of the downloaded code. I’d still like to mention that the modified slowHash() function fixes both of them, but I’ll try to make it clear that you’d have to backport the changes to a previous version of the app by hand if you wanted to test both.

As ever, I really appreciate the feedback!

Where Next?

Popular Pragmatic Bookshelf topics Top

johnp
Running the examples in chapter 5 c under pytest 5.4.1 causes an AttributeError: ‘module’ object has no attribute ‘config’. In particula...
New
jeffmcompsci
Title: Design and Build Great Web APIs - typo “https://company-atk.herokuapp.com/2258ie4t68jv” (page 19, third bullet in URL list) Typo:...
New
jesse050717
Title: Web Development with Clojure, Third Edition, pg 116 Hi - I just started chapter 5 and I am stuck on page 116 while trying to star...
New
joepstender
The generated iex result below should list products instead of product for the metadata. (page 67) iex> product = %Product{} %Pento....
New
Chrichton
Dear Sophie. I tried to do the “Authorization” exercise and have two questions: When trying to plug in an email-service, I found the ...
New
patoncrispy
I’m new to Rust and am using this book to learn more as well as to feed my interest in game dev. I’ve just finished the flappy dragon exa...
New
brunogirin
When trying to run tox in parallel as explained on page 151, I got the following error: tox: error: argument -p/–parallel: expected one...
New
creminology
Skimming ahead, much of the following is explained in Chapter 3, but new readers (like me!) will hit a roadblock in Chapter 2 with their ...
New
mert
AWDWR 7, page 152, page 153: Hello everyone, I’m a little bit lost on the hotwire part. I didn’t fully understand it. On page 152 @rub...
New
andreheijstek
After running /bin/setup, the first error was: The foreman' command exists in these Ruby versions: That was easy to fix: gem install fore...
New

Other popular topics Top

Exadra37
I am thinking in building or buy a desktop computer for programing, both professionally and on my free time, and my choice of OS is Linux...
New
AstonJ
poll poll Be sure to check out @Dusty’s article posted here: An Introduction to Alternative Keyboard Layouts It’s one of the best write-...
New
New
mafinar
Crystal recently reached version 1. I had been following it for awhile but never got to really learn it. Most languages I picked up out o...
New
New
Margaret
Hello everyone! This thread is to tell you about what authors from The Pragmatic Bookshelf are writing on Medium.
1143 25883 760
New
AstonJ
Saw this on TikTok of all places! :lol: Anyone heard of them before? Lite:
New
First poster: bot
zig/http.zig at 7cf2cbb33ef34c1d211135f56d30fe23b6cacd42 · ziglang/zig. General-purpose programming language and toolchain for maintaini...
New
AstonJ
This is a very quick guide, you just need to: Download LM Studio: https://lmstudio.ai/ Click on search Type DeepSeek, then select the o...
New
AstonJ
Curious what kind of results others are getting, I think actually prefer the 7B model to the 32B model, not only is it faster but the qua...
New

Sub Categories: