Created
October 29, 2009 21:03
-
-
Save andreimaxim/221821 to your computer and use it in GitHub Desktop.
Un mini-scheduler scris in Ruby pentru a rula task-uri in paralel.
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
## Ruleaza bucati de Ruby pe un numar maxim de thread-uri | |
# | |
# Utilizare: | |
# | |
# runner = Scheduler.new :max_threads => 20 | |
# ary.each { |item| runner.queue { stuff_to_do_in_paralel } } | |
# runner.start | |
# | |
# Codul este complet netestat. | |
class Scheduler | |
MAX_THREADS = 15 | |
FREQUENCY = 1 | |
def initialize(opt = {}) | |
@max_threads = opt[:max_threads] || MAX_THREADS | |
@frequency = opt[:interval] || FREQUENCY | |
@running_threads = 0 | |
@working_queue = Queue.new | |
@lock = Mutex.new | |
@logger = Logger.new(STDOUT) | |
@logger.datetime_format = "%H:%M:%S" | |
end | |
def queue(&block) | |
@working_queue.push block | |
end | |
def start | |
while @working_queue.size > 0 | |
if @running_threads < @max_threads | |
spawn :worker => @working_queue.pop | |
else | |
# Asteapta @frequency secunde si apoi resume | |
sleep @frequency | |
end | |
end | |
Thread.list.each { |t| t.join unless t == Thread.main or t == Thread.current } | |
end | |
private | |
def spawn(args = {}) | |
@running_threads += 1 | |
logger "Running threads: #{@running_threads}, queue size: #{@working_queue.size}" | |
proc = args[:worker] | |
Thread.new do | |
proc.call | |
@lock.synchronize { @running_threads -= 1 } | |
end | |
end | |
def logger(msg) | |
@logger.info(msg) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment