Created
December 29, 2011 14:13
-
-
Save monzou/1534261 to your computer and use it in GitHub Desktop.
Ruby でファイルをモニタリングするとこんな感じなのかなぁ?
This file contains hidden or 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 'fssm' | |
require 'thread' | |
PATH = "/Users/monzou/sample.txt" | |
class Monitor | |
def start(&block) | |
puts "start monitoring @ #{Thread.current}" | |
monitor = FSSM::Monitor.new | |
monitor.path(File.dirname(PATH), File.basename(PATH)) do | |
update(&block) | |
end | |
thread = Thread.new { monitor.run } | |
thread.join | |
end | |
end | |
class Model | |
attr_accessor :value | |
def initialize | |
@value = "" | |
end | |
end | |
puts "boot @ #{Thread.current}" | |
model = Model.new | |
monitor = Monitor.new | |
mutex = Mutex.new | |
monitor.start do | |
puts "update @ #{Thread.current}" | |
# is it thread-safe ? | |
mutex.synchronize do | |
model.value << "+" | |
puts model.value | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment