lib/picky/cores.rb in picky-3.1.7 vs lib/picky/cores.rb in picky-3.1.8
- old
+ new
@@ -14,62 +14,52 @@
# end
#
# Options include:
# * max: Maximum # of processors to use. Default is all it can get.
#
- def self.forked ary_or_generator, options = {}
- return if ary_or_generator.empty?
+ def self.forked elements, options = {}, &block
+ return if elements.empty?
raise "Block argument needed when running Cores.forked" unless block_given?
- ary_or_generator = ary_or_generator.sort_by { rand } if options[:randomly]
- generator = ary_or_generator.each
-
- # Don't fork if there's just one element.
+ # Note: Not using a generator because Enumerator#each
+ # is exhibiting problems in some Rubies on some OSs
+ # (see e.g. http://redmine.ruby-lang.org/issues/5003).
#
- if generator.inject(0) { |total, element| total + 1 } == 1
- generator.each do |element|
- yield element # THINK yield generator.next results in trouble. Why?
- end
- return
- end
+ elements = elements.dup
+ elements = elements.sort_by { rand } if options[:randomly]
# Get the maximum number of processors.
#
- max = max_processors options
- currently_processing = 0
+ max = max_processors options
+ processing = 0
- #
- #
loop do
- # Ramp it up to num processors.
- #
- while currently_processing < max
-
- currently_processing = currently_processing + 1
-
- element = nil
- begin
- element = generator.next
- rescue StopIteration => si
- break
- end
+ while processing < max
+ # Get the next element
+ #
+ element = elements.shift
break unless element
+ processing += 1
+ # Fork and yield.
+ #
Process.fork do
- sleep 0.01*currently_processing
- yield element
+ sleep 0.05*processing
+ block.call element
end
-
end
+ # Block and wait for any child to finish.
+ #
begin
- Process.wait 0 # Block and wait for any child to finish.
+ Process.wait 0
rescue Errno::ECHILD => e
break
ensure
- currently_processing = currently_processing - 1
+ processing -= 1
end
end
+
end
# Return the number of maximum usable processors.
#
def self.max_processors options = {}
\ No newline at end of file