Created
November 1, 2012 19:49
-
-
Save havenwood/3996000 to your computer and use it in GitHub Desktop.
Maglev Queue
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 'maglev/rcqueue' | |
Store = Maglev::PERSISTENT_ROOT | |
Maglev.abort_transaction | |
Store[:queue] ||= RCQueue.new | |
20.times do |t| | |
Store[:queue] << Proc.new do | |
sleep 1 | |
puts "job ##{t}" | |
end | |
end | |
Maglev.commit_transaction | |
loop do | |
Maglev.abort_transaction | |
until Store[:queue].empty? | |
Thread.new do | |
Store[:queue].shift.call | |
end | |
Maglev.commit_transaction | |
end | |
sleep 0.5 | |
end |
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
# Add more tasks from another instance of maglev irb: | |
Store = Maglev::PERSISTENT_ROOT | |
Maglev.abort_transaction | |
5.times do | |
Store[:queue] << Proc.new do | |
sleep 1 | |
puts 'another task!' | |
end | |
end | |
Maglev.commit_transaction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment