WIP: Ractors #25

Draft
bustikiller wants to merge 3 commits from ractors into main

51
lib/tasks/ractors.rake Normal file
View File

@ -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