Created
December 20, 2013 01:01
-
-
Save mdunsmuir/8048869 to your computer and use it in GitHub Desktop.
simple, stupid thread pool for running system calls
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
MAX_THREADS = 4 | |
def self.run_threaded_systems(tasks) | |
queued_tasks = tasks.each_with_object(Queue.new) do |task, queue| | |
queue.push(task) | |
end | |
MAX_THREADS.times.inject(Array.new) { |threads| | |
threads << Thread.new(queued_tasks) { |queue| | |
while task = queued_tasks.pop(true) rescue nil | |
puts("running command #{task.join(" ")}") | |
system(*task) | |
end | |
} | |
}.each { |thread| thread.join } | |
nil | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment