Module: Parallel

### Home Classes Methods

### In Files

- lib/parallel.rb
- lib/parallel/processor_count.rb
- lib/parallel/version.rb

### Namespace

- MODULE Parallel::ProcessorCount
- CLASS Parallel::Break
- CLASS Parallel::DeadWorker
- CLASS Parallel::ExceptionWrapper
- CLASS Parallel::JobFactory
- CLASS Parallel::Kill
- CLASS Parallel::UserInterruptHandler
- CLASS Parallel::Worker

### Methods

- ::each
- ::each_with_index
- ::in_processes
- ::in_threads
- ::map
- ::map_with_index

### Class/Module Index

Quicksearch

- Parallel
- Parallel::Break
- Parallel::DeadWorker
- Parallel::ExceptionWrapper
- Parallel::JobFactory
- Parallel::Kill
- Parallel::ProcessorCount
- Parallel::UserInterruptHandler
- Parallel::Worker

No matching classes.

# Parallel

### Constants

Stop
VERSION

### Public Class Methods

each(array, options={}, &block) click to toggle source

    # File lib/parallel.rb, line 193
    def each(array, options={}, &block)
      map(array, options.merge(:preserve_results => false), &block)
      array
    end

each_with_index(array, options={}, &block) click to toggle source

    # File lib/parallel.rb, line 198
    def each_with_index(array, options={}, &block)
      each(array, options.merge(:with_index => true), &block)
    end

in_processes(options = {}, &block) click to toggle source

    # File lib/parallel.rb, line 187
    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}) click to toggle source

    # File lib/parallel.rb, line 180
    def in_threads(options={:count => 2})
      count, _ = extract_count_from_options(options)
      Array.new(count).each_with_index.map do |_, i|
        Thread.new { yield(i) }
      end.map!(&:value)
    end

map(source, options = {}, &block) click to toggle source

    # File lib/parallel.rb, line 202
    def map(source, 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
          warn "Process.fork is not supported by this Ruby"
          size = 0
        end
      end

      job_factory = JobFactory.new(source, options[:mutex])
      size = [job_factory.size, size].min

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

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

map_with_index(array, options={}, &block) click to toggle source

    # File lib/parallel.rb, line 236
    def map_with_index(array, options={}, &block)
      map(array, options.merge(:with_index => true), &block)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.