Module: Parallel

Extended by:
ProcessorCount
Defined in:
lib/parallel.rb,
lib/parallel/version.rb,
lib/parallel/processor_count.rb

Defined Under Namespace

Modules: ProcessorCount Classes: Break, DeadWorker, ExceptionWrapper, ItemWrapper, Kill, Worker

Constant Summary collapse

Stop =
Object.new
INTERRUPT_SIGNAL =
:SIGINT
VERSION =
Version = '1.3.3'

Class Method Summary collapse

Methods included from ProcessorCount

physical_processor_count, processor_count

Class Method Details

.each(array, options = {}, &block) ⇒ Object



151
152
153
154
# File 'lib/parallel.rb', line 151

def each(array, options={}, &block)
  map(array, options.merge(:preserve_results => false), &block)
  array
end

.each_with_index(array, options = {}, &block) ⇒ Object



156
157
158
# File 'lib/parallel.rb', line 156

def each_with_index(array, options={}, &block)
  each(array, options.merge(:with_index => true), &block)
end

.in_processes(options = {}, &block) ⇒ Object



145
146
147
148
149
# File 'lib/parallel.rb', line 145

def in_processes(options = {}, &block)
  count, options = extract_count_from_options(options)
  count ||= processor_count
  map(0...count, options.merge(:in_processes => count), &block)
end

.in_threads(options = {:count => 2}) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/parallel.rb', line 128

def in_threads(options={:count => 2})
  count, options = extract_count_from_options(options)

  out = []
  threads = []

  count.times do |i|
    threads[i] = Thread.new do
      out[i] = yield(i)
    end
  end

  kill_on_ctrl_c(threads, options) { wait_for_threads(threads) }

  out
end

.map(array, options = {}, &block) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/parallel.rb', line 160

def map(array, options = {}, &block)
  options[:mutex] = Mutex.new

  if RUBY_PLATFORM =~ /java/ and not options[:in_processes]
    method = :in_threads
    size = options[method] || processor_count
  elsif options[:in_threads]
    method = :in_threads
    size = options[method]
  else
    method = :in_processes
    if Process.respond_to?(:fork)
      size = options[method] || processor_count
    else
      $stderr.puts "Warning: Process.fork is not supported by this Ruby"
      size = 0
    end
  end

  items = ItemWrapper.new(array, options[:mutex])

  size = [items.producer? ? size : items.size, size].min

  options[:return_results] = (options[:preserve_results] != false || !!options[:finish])
  add_progress_bar!(items, options)

  if size == 0
    work_direct(items, options, &block)
  elsif method == :in_threads
    work_in_threads(items, options.merge(:count => size), &block)
  else
    work_in_processes(items, options.merge(:count => size), &block)
  end
end

.map_with_index(array, options = {}, &block) ⇒ Object



195
196
197
# File 'lib/parallel.rb', line 195

def map_with_index(array, options={}, &block)
  map(array, options.merge(:with_index => true), &block)
end