From 7b8cdd2276d38995cdda78c7732ac94f881c500b Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Fri, 2 Aug 2024 17:29:04 +0200 Subject: [PATCH] WIP ractors --- lib/tasks/ractors.rake | 51 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 lib/tasks/ractors.rake diff --git a/lib/tasks/ractors.rake b/lib/tasks/ractors.rake new file mode 100644 index 0000000..827d670 --- /dev/null +++ b/lib/tasks/ractors.rake @@ -0,0 +1,51 @@ +namespace :ractors do + desc 'Run the Ractors example' + task run: :environment do + list = (1..20) + number_of_ractors = Concurrent.processor_count + + ractors = number_of_ractors.times.map do |i| + Ractor.new(name: "Ractor #{i}") do + loop do + number = Ractor.receive + puts "Processing #{number} in #{Ractor.current.name}" + Ractor.yield "eureka" if number == 17 + end + end + end.cycle + + list.each do |i| + ractors.next.send(i) + end + + puts "all enqueued" + + binding.pry + + Ractor.select(*ractors) + + # binding.pry + + # number_of_ractors.times do |i| + # r = Ractor.new(name: "Ractor #{i}") do + # Ractor.receive.each do |i| + # puts "Processing index #{i} in #{Ractor.current.name}" + # end + # end + + # r.send((i * 250)...((i + 1) * 250)) + # end + + # ractor = Ractor.new do + # loop do + # puts 'I will receive a message soon' + # msg = Ractor.receive + # puts 'I will return a tripple of what I receive' + # Ractor.yield(msg * 3) + # end + # end + + # heavy_calculation.send(15) + # puts heavy_calculation.take + end +end