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.2'
Class Method Summary
collapse
physical_processor_count, processor_count
Class Method Details
.each(array, options = {}, &block) ⇒ Object
150
151
152
153
|
# File 'lib/parallel.rb', line 150
def each(array, options={}, &block)
map(array, options.merge(:preserve_results => false), &block)
array
end
|
.each_with_index(array, options = {}, &block) ⇒ Object
155
156
157
|
# File 'lib/parallel.rb', line 155
def each_with_index(array, options={}, &block)
each(array, options.merge(:with_index => true), &block)
end
|
.in_processes(options = {}, &block) ⇒ Object
144
145
146
147
148
|
# File 'lib/parallel.rb', line 144
def in_processes(options = {}, &block)
count, options = (options)
count ||= processor_count
map(0...count, options.merge(:in_processes => count), &block)
end
|
.in_threads(options = {:count => 2}) ⇒ Object
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
# File 'lib/parallel.rb', line 127
def in_threads(options={:count => 2})
count, 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
159
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
|
# File 'lib/parallel.rb', line 159
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
194
195
196
|
# File 'lib/parallel.rb', line 194
def map_with_index(array, options={}, &block)
map(array, options.merge(:with_index => true), &block)
end
|