page 205, paragraph after the code, line 2 :
then blocks at Ractor.receive. Only then do we create the reading ractor
Because of “Only then” I thought that creating the second Ractor could only take place when the first is waiting on receive
. But the trace shows that both Ractors are created, then the blocks are scheduled.
On page 204 (par. 5, code for one, line 1) I liked “Moving down the file”, for a possible replacement of “Only then”.
Having said that, it’s impressive to see the reader sending several times without waiting for a take.
def displayWord(p_word)
p_word.nil? ? "^nil^" : "|#{p_word}|"
end
puts "*** (main) about to create counter"
counter = Ractor.new(name: "counter") do
puts ">>> ccccc in counter block"
result = Hash.new(0)
while (word = Ractor.receive)
puts "ccccc in counter received word=#{displayWord(word)}"
result[word] += 1
puts "ccccc looping"
end
puts "ccccc in counter while ended word=#{displayWord(word)}"
result
end
puts "*** (main) about to create reader"
Ractor.new(counter, name: "reader") do |counter|
puts ">>> rrrrr in reader block"
File.foreach("testfile") do |line|
puts "rrrrr line=#{line}"
line.scan(/\w+/) do |word|
puts "rrrrr about to send word=#{displayWord(word)}"
counter.send(word.downcase)
puts "rrrrr after send"
end
end
puts "rrrrr about to send nil"
counter.send(nil)
end
puts "*** (main) about to take counter"
counts = counter.take
counts.keys.sort.each { |k| print "#{k}:#{counts[k]} " }
puts
% ruby -w ractor_word_count_flipped.rb
*** (main) about to create counter
<internal ... experimental ...
*** (main) about to create reader
*** (main) about to take counter
>>> ccccc in counter block
>>> rrrrr in reader block
rrrrr line=This is line one
rrrrr about to send word=|This|
rrrrr after send
rrrrr about to send word=|is|
rrrrr after send
rrrrr about to send word=|line|
ccccc in counter received word=|this|
...