Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
def source(generator, target:, name: nil)
Ractor.new(generator, target, name: name) do |generator, target|
loop do
target.send generator.next
end
end
end
def buffer
Ractor.new do
loop do
Ractor.yield Ractor.receive
end
end
end
def worker(behaviour, source:, name: nil)
Ractor.new(behaviour, source, name: name) do |behaviour, source|
state = behaviour.init_state
loop do
state = behaviour.receive(source.take, state)
end
end
end
require "prime"
require_relative "./components"
module RandInt
MAX_INT = 10**23
def self.next
sleep rand * 0.2
rand(MAX_INT) + 1
end
end
module PrimeTest
def self.init_state
Hash.new # cache
end
def self.receive(msg, cache)
case [msg, cache[msg]]
in m, nil
cache[m] = m.prime?
in m, is_prime
puts "cache hit for #{m}"
end
puts "#{Ractor.current.name}: #{msg} #{cache[msg]}"
cache
end
end
bfr = buffer
(1..2).map { |i|
source(RandInt, target: bfr, name: "source_#{i}")
}
(1..5).map { |i|
worker(PrimeTest, source: bfr, name: "worker_#{i}")
}
sleep
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment