Skip to content

Instantly share code, notes, and snippets.

@watsy0007
Created October 18, 2016 03:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save watsy0007/b30758a7c8b0921ad12a6e058512c02c to your computer and use it in GitHub Desktop.
Save watsy0007/b30758a7c8b0921ad12a6e058512c02c to your computer and use it in GitHub Desktop.
Producer Consumer with Actors
require 'celluloid/current'
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(1_000)
@watsy0007
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment