Created
October 18, 2016 03:10
-
-
Save watsy0007/b30758a7c8b0921ad12a6e058512c02c to your computer and use it in GitHub Desktop.
Producer Consumer with Actors
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
原文 Produce-Consumer-with-Actors