Skip to content

Instantly share code, notes, and snippets.

@mdunsmuir
Created December 20, 2013 01:01
Show Gist options
  • Save mdunsmuir/8048869 to your computer and use it in GitHub Desktop.
Save mdunsmuir/8048869 to your computer and use it in GitHub Desktop.
simple, stupid thread pool for running system calls
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