Class: InThreads

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/in_threads.rb

Overview

Run Enumerable methods with blocks in threads

Defined Under Namespace

Classes: Pool, QueueEnum

Constant Summary collapse

INCOMPATIBLE_METHODS =
%w[
  inject reduce
  max min minmax sort
  entries to_a to_set to_h
  drop take
  first
  include? member?
  each_with_object
  chunk chunk_while slice_before slice_after slice_when
  lazy
].map(&:to_sym)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enumerable, thread_count = 10, &block) ⇒ InThreads

Returns a new instance of InThreads.



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/in_threads.rb', line 34

def initialize(enumerable, thread_count = 10, &block)
  super(enumerable)
  @enumerable, @thread_count = enumerable, thread_count.to_i
  unless enumerable.is_a?(Enumerable)
    fail ArgumentError, '`enumerable` should include Enumerable.'
  end
  if thread_count < 2
    fail ArgumentError, '`thread_count` can\'t be less than 2.'
  end
  each(&block) if block
end

Instance Attribute Details

#enumerableObject (readonly)

Returns the value of attribute enumerable.



33
34
35
# File 'lib/in_threads.rb', line 33

def enumerable
  @enumerable
end

#thread_countObject (readonly)

Returns the value of attribute thread_count.



33
34
35
# File 'lib/in_threads.rb', line 33

def thread_count
  @thread_count
end

Class Method Details

.use(runner, options) ⇒ Object

Specify runner to use

use :run_in_threads_use_block_result, :for => %w[all? any? none? one?]

‘:for` is required `:ignore_undefined` ignores methods which are not present in `Enumerable.instance_methods`



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/in_threads.rb', line 59

def use(runner, options)
  methods = Array(options[:for])
  fail 'no methods provided using :for option' if methods.empty?
  ignore_undefined = options[:ignore_undefined]
  methods.each do |method|
    next if ignore_undefined && !enumerable_method?(method)
    class_eval <<-RUBY
      def #{method}(*args, &block)
        if block
          #{runner}(:#{method}, *args, &block)
        else
          enumerable.#{method}(*args)
        end
      end
    RUBY
  end
end

Instance Method Details

#grep(*args, &block) ⇒ Object

Special case method, works by applying ‘run_in_threads_use_block_result` with map on enumerable returned by blockless run



115
116
117
118
119
120
121
# File 'lib/in_threads.rb', line 115

def grep(*args, &block)
  if block
    self.class.new(enumerable.grep(*args), thread_count).map(&block)
  else
    enumerable.grep(*args)
  end
end

#grep_v(*args, &block) ⇒ Object

Special case method, works by applying ‘run_in_threads_use_block_result` with map on enumerable returned by blockless run



126
127
128
129
130
131
132
# File 'lib/in_threads.rb', line 126

def grep_v(*args, &block)
  if block
    self.class.new(enumerable.grep_v(*args), thread_count).map(&block)
  else
    enumerable.grep_v(*args)
  end
end

#in_threads(thread_count = 10, &block) ⇒ Object

Creates new instance using underlying enumerable and new thread_count



47
48
49
# File 'lib/in_threads.rb', line 47

def in_threads(thread_count = 10, &block)
  self.class.new(enumerable, thread_count, &block)
end

#with_progress(title = nil, length = nil, &block) ⇒ Object

befriend with progress gem



136
137
138
# File 'lib/in_threads.rb', line 136

def with_progress(title = nil, length = nil, &block)
  ::Progress::WithProgress.new(self, title, length, &block)
end