-
-
Save halorgium/b7689c261ec91c462f80 to your computer and use it in GitHub Desktop.
distributed lock aka ConditionVariable
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
# this requires the master branch of Celluloid | |
require 'celluloid' | |
class Worker | |
include Celluloid | |
def initialize(condition, number) | |
@condition = condition | |
@number = number | |
async.work | |
end | |
def work | |
Celluloid::Logger.info "worker #{@number.inspect} is waiting" | |
@condition.wait | |
Celluloid::Logger.info "worker #{@number.inspect} is working" | |
sleep 1 | |
Celluloid::Logger.info "worker #{@number.inspect} is finished" | |
ensure | |
Celluloid::Logger.info "worker #{@number.inspect} is signalling" | |
@condition.signal | |
async.work | |
end | |
end | |
condition = Celluloid::Condition.new | |
10.times do |number| | |
Worker.new(condition, number) | |
end | |
# start the work | |
condition.signal | |
sleep |
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
source "https://rubygems.org" | |
gem "celluloid", github: "celluloid/celluloid" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment