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: Splitter, ThreadLimiter

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
].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.



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

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.



112
113
114
# File 'lib/in_threads.rb', line 112

def enumerable
  @enumerable
end

#thread_countObject (readonly)

Returns the value of attribute thread_count.



112
113
114
# File 'lib/in_threads.rb', line 112

def thread_count
  @thread_count
end

Class Method Details

.use(runner, options) ⇒ Object

Specify runner to use

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

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



138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/in_threads.rb', line 138

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)
        #{runner}(:#{method}, *args, &block)
      end
    RUBY
  end
end

Instance Method Details

#grep(*args, &block) ⇒ Object

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



190
191
192
193
194
195
196
# File 'lib/in_threads.rb', line 190

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_consecutive` with map on enumerable returned by blockless run



201
202
203
204
205
206
207
# File 'lib/in_threads.rb', line 201

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



126
127
128
# File 'lib/in_threads.rb', line 126

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



211
212
213
# File 'lib/in_threads.rb', line 211

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