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



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/in_threads.rb', line 36

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.



35
36
37
# File 'lib/in_threads.rb', line 35

def enumerable
  @enumerable
end

#thread_countObject (readonly)

Returns the value of attribute thread_count.



35
36
37
# File 'lib/in_threads.rb', line 35

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`



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/in_threads.rb', line 62

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, __FILE__, __LINE__ + 1
      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



121
122
123
124
125
126
127
# File 'lib/in_threads.rb', line 121

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



132
133
134
135
136
137
138
# File 'lib/in_threads.rb', line 132

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



50
51
52
# File 'lib/in_threads.rb', line 50

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



142
143
144
# File 'lib/in_threads.rb', line 142

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