Class: ThreadPool::Executor

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/thread_pool.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(queue, mutex) ⇒ Executor

Returns a new instance of Executor.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/thread_pool.rb', line 12

def initialize(queue, mutex)
  @thread = Thread.new do
    loop do
      mutex.synchronize { @tuple = queue.shift }
      if @tuple
        debug "Executor: processing #{@tuple.hash}"
        args, block = @tuple
        @active = true
        begin
          block.call(*args)
        rescue Exception => e
          error e.message
          error e.backtrace.join("\n")
        end
        block.complete = true
        debug "Executor: complete   #{@tuple.hash}"
      else
        @active = false
        sleep 0.01
      end
    end
  end
end

Instance Attribute Details

#activeObject (readonly)

Returns the value of attribute active.



10
11
12
# File 'lib/thread_pool.rb', line 10

def active
  @active
end

Instance Method Details

#closeObject



36
37
38
# File 'lib/thread_pool.rb', line 36

def close
  @thread.exit
end