Module: PMap

Included in:
Enumerable
Defined in:
lib/pmap/thread_pool.rb,
lib/pmap.rb

Overview

Internal: A thread pool for managing the threads used by the pmap functions

Example:

pool = PMap::ThreadPool.new(16)
array.each do |item|
  pool.schedule do
    item.some_io_intense_operation
  end
end

pool.shutdown

Defined Under Namespace

Classes: ThreadPool

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/pmap.rb', line 72

def self.included(base)
  base.class_eval do

    # @see PMap#pmap
    def pmap(thread_count=nil, &proc)
      return self unless proc

      array_mutex = Mutex.new
      Array.new.tap do |result|
        peach_with_index(thread_count) do |item, index|
          value = proc.call(item)
          array_mutex.synchronize { result[index] = value }
        end
      end
    end

    # @see PMap#peach
    def peach(thread_count=nil, &proc)
      if proc
        peach_with_index(thread_count) do |item, index|
          proc.call(item)
        end
      end
      self
    end

    # @see PMap#peach_with_index
    def peach_with_index(thread_count=nil, &proc)
      return each_with_index unless proc

      thread_count ||= $pmap_default_thread_count
      pool = ThreadPool.new(thread_count)

      each_with_index do |item, index|
        pool.schedule(item, index, &proc)
      end
      pool.shutdown
      self
    end

    # @see PMap#flat_pmap
    def flat_pmap(thread_count=nil, &proc)
      return self unless proc

      pmap(thread_count, &proc).flatten(1)
    end
  end
end

Instance Method Details

#flat_pmapArray

Parallel #flat_map for any Enumerable

When a block is given, each item is yielded to the block in a separate thread. When no block is given, an Enumerable is returned.

Parameters:

  • thread_count (Integer)

    maximum number of threads to create (optional)

Returns:

  • (Array)

    of mapped objects

See Also:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/pmap.rb', line 72

def self.included(base)
  base.class_eval do

    # @see PMap#pmap
    def pmap(thread_count=nil, &proc)
      return self unless proc

      array_mutex = Mutex.new
      Array.new.tap do |result|
        peach_with_index(thread_count) do |item, index|
          value = proc.call(item)
          array_mutex.synchronize { result[index] = value }
        end
      end
    end

    # @see PMap#peach
    def peach(thread_count=nil, &proc)
      if proc
        peach_with_index(thread_count) do |item, index|
          proc.call(item)
        end
      end
      self
    end

    # @see PMap#peach_with_index
    def peach_with_index(thread_count=nil, &proc)
      return each_with_index unless proc

      thread_count ||= $pmap_default_thread_count
      pool = ThreadPool.new(thread_count)

      each_with_index do |item, index|
        pool.schedule(item, index, &proc)
      end
      pool.shutdown
      self
    end

    # @see PMap#flat_pmap
    def flat_pmap(thread_count=nil, &proc)
      return self unless proc

      pmap(thread_count, &proc).flatten(1)
    end
  end
end

#peachvoid

This method returns an undefined value.

Parallel #each for any Enumerable.

When a block is given, each item is yielded to the block in a separate thread. When no block is given, an Enumerable is returned.

Parameters:

  • thead_count (Integer)

    maximum number of threads to create (optional)

See Also:



# File 'lib/pmap.rb', line 24


#peach_with_indexvoid

This method returns an undefined value.

Parallel #each_with_index for any Enumerable.

When a block is given, each item is yielded to the block in a separate thread. When no block is given, an Enumerable is returned.

Parameters:

  • thread_count (Integer)

    maximum number of threads to create (optional)

See Also:



# File 'lib/pmap.rb', line 40


#pmapArray

Parallel #map for any Enumerable.

When a block is given, each item is yielded to the block in a separate thread. When no block is given, an Enumerable is returned.

Parameters:

  • thread_count (Integer)

    maximum number of threads to create (optional)

Returns:

  • (Array)

    of mapped objects

See Also:



# File 'lib/pmap.rb', line 8