-
-
Save anonymous/cce6c5d27cd5eddd02e8 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
Thread.abort_on_exception = true | |
class JobQueue | |
def initialize options = {} | |
@q = Queue.new | |
@per_minute = options[:per_minute] | |
Thread.new do | |
loop do | |
before = Time.now | |
@per_minute.times do | |
Thread.new(&@q.pop) | |
end | |
diff = Time.now-before | |
sleep [60-diff,0].max | |
end | |
end | |
end | |
def add(&pr) | |
@q << pr | |
end | |
end | |
jq = JobQueue.new per_minute:6 | |
loop do | |
jq.add { puts "I should be executed max. 6 times per minute" } | |
sleep 0.001 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment