Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Producer Consumer with Actors
require 'celluloid/autostart'
class Cook
include Celluloid
def produce(manager)
manager.async.inform("produced")
end
end
class Consumer
include Celluloid
def consume(manager)
manager.async.inform("consumed")
end
end
class Manager
include Celluloid
def initialize
@items = []
@producer = Cook.new
@consumer = Consumer.new
end
def work
@producer.async.produce(Actor.current)
end
def inform(message)
if message == "produced"
@items.push message
puts Thread.current.to_s + " Produced item .... Stock : " + @items.size.to_s
@consumer.async.consume(Actor.current)
end
if message == "consumed"
@items.pop
puts Thread.current.to_s + " Consumed item .... Stock : " + @items.size.to_s
work
end
end
end
manager = Manager.new
manager.async.work
sleep
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.