lib/parallel.rb in grosser-parallel-0.2.0 vs lib/parallel.rb in grosser-parallel-0.3.0
- old
+ new
@@ -47,19 +47,50 @@
collectors.each{|c|c.join}
out.map{|x| Marshal.load(x)} #deserialize
end
+ def self.map(array, options={})
+ count = if options[:in_threads]
+ method = 'in_threads'
+ options[:in_threads]
+ else
+ method = 'in_processes'
+ options[:in_processes] || processor_count
+ end
+
+ results = []
+ in_groups_of(array, count).each do |group|
+ results += send(method, group.size) do |i|
+ yield group[i]
+ end
+ end
+ results
+ end
+
def self.processor_count
case RUBY_PLATFORM
when /darwin/
`hwprefs cpu_count`.to_i
when /linux/
`cat /proc/cpuinfo | grep processor | wc -l`.to_i
end
end
private
+
+ def self.in_groups_of(array, count)
+ results = []
+ loop do
+ slice = array[(results.size * count)...((results.size+1) * count)]
+ if slice.nil? or slice.empty?
+ break
+ else
+ results << slice
+ end
+ end
+ results
+ end
#handle user interrup (Ctrl+c)
def self.kill_on_ctrl_c(pids)
Signal.trap 'SIGINT' do
STDERR.puts "Parallel execution interrupted, exiting ..."
\ No newline at end of file