#!/usr/bin/ruby # Batch process distributer (per-argument) for multi-core systems, version 1.0 # # Copyright Clay Barnes (clay@hci-matters.com) # Licenced under the GNU Public Licence Version 3 or later # # If you find this useful (or find a bug), please drop me an email. I know it's # not feature complete, but hey, quick 'n' dirty does the job sometimes! def print_usage; puts "Usage: distributer num_processes command var1 [ var2... ]\nUse %VAR to reference the current variable (ex. echo %VAR)"; exit; end def print_seperator; puts "\n================================================================================\n\n"; end if ARGV.size < 3 then print_usage end # Too few arguments. begin # Initalize variables. Errors are assumed to be the user's fault ;-) num_threads = 0 + ARGV[0].to_i if num_threads<1 then print_usage; exit; end puts "Number of concurrent processes: " + num_threads.to_s command = ARGV[1] puts "Using command '" + command.gsub("%VAR", "[VARIABLE_HERE]")+"'" variables = ARGV[2..-1] puts "Using the following variables:\n"+"'"+ variables*"', '" +"'" print_seperator rescue # Oops, bug out! print_usage end begin # Parallelize [num_threads] processes. index = 0 # Index of current variable while num_threads > 0 and variables[index] puts "Starting: " + command.gsub("%VAR",variables[index]) print_seperator Process.fork{system(command.gsub("%VAR",variables[index]))} num_threads = num_threads - 1 index = index + 1 end while true Process.wait(-1, 0) # Whenever a process exits, spawn a new one. if variables[index] # Quite creating processes upon nil! puts "Starting: "+command.gsub("%VAR",variables[index]) print_seperator Process.fork{system(command.gsub("%VAR",variables[index]))} index = index + 1 end end rescue Errno::ECHILD # Once finished creating processes, and all have exited. puts "All processes completed!" end