Created
August 28, 2013 14:41
-
-
Save yannickwurm/6366767 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
def YWSystemTools.runMultithreaded(commandArray) | |
# runs commands on N processors at a time - can take more jobs & launches them sequentially. | |
if commandArray.length > 1 | |
$log.info("#{commandArray.length} commands to run") | |
elsif commandArray.length == 1 | |
$log.warn("only one command!") | |
else | |
raise ArgumentError, 'No commands to run!' | |
end | |
#determining how many procs to use | |
threads = [] | |
numberOfProcessorsToUse = YWSystemTools.numberOfAvailableProcessors | |
$log.info("Will use #{numberOfProcessorsToUse} processors") | |
if numberOfProcessorsToUse == 1 | |
$log.warn("Not running multithreaded because there is only one processor") | |
elsif numberOfProcessorsToUse == 0 | |
$log.warn("No processors available - will nevertheless use one") | |
numberOfProcessorsToUse == 1 | |
end | |
if (commandArray.length < numberOfProcessorsToUse) # we need less | |
numberOfProcessorsToUse = commandArray.length | |
end | |
# we need one name per processor & have some margin | |
('proc00001'..'proc10000').entries[1..numberOfProcessorsToUse].each do |processor| | |
threads << Thread.new(processor) do |threadid| | |
until commandArray.empty? | |
commandToRun = commandArray.pop | |
$log.info('running:' + commandToRun) | |
## %x[#{commandToRun}] | |
stdout = %x[#{commandToRun}] | |
print(stdout) | |
end | |
end | |
end | |
# wait until all are done. | |
threads.each { |aThread| aThread.join } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment