Class: Katte::ThreadPool
- Inherits:
-
Object
- Object
- Katte::ThreadPool
- Defined in:
- lib/katte/thread_pool.rb
Instance Attribute Summary collapse
-
#threads ⇒ Object
readonly
Returns the value of attribute threads.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(threads_num = 4, logger = Katte.app.logger) ⇒ ThreadPool
constructor
A new instance of ThreadPool.
- #join ⇒ Object
- #push(&procedure) ⇒ Object
- #run ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(threads_num = 4, logger = Katte.app.logger) ⇒ ThreadPool
Returns a new instance of ThreadPool.
10 11 12 13 14 |
# File 'lib/katte/thread_pool.rb', line 10 def initialize(threads_num = 4, logger = Katte.app.logger) @queue = Queue.new @threads_num = threads_num @logger = logger end |
Instance Attribute Details
#threads ⇒ Object (readonly)
Returns the value of attribute threads.
9 10 11 |
# File 'lib/katte/thread_pool.rb', line 9 def threads @threads end |
Class Method Details
.instance ⇒ Object
5 6 7 |
# File 'lib/katte/thread_pool.rb', line 5 def self.instance @instance ||= new.tap(&:run) end |
Instance Method Details
#join ⇒ Object
37 38 39 |
# File 'lib/katte/thread_pool.rb', line 37 def join @threads.each {|t| t.join} end |
#push(&procedure) ⇒ Object
29 30 31 |
# File 'lib/katte/thread_pool.rb', line 29 def push &procedure @queue.push procedure end |
#run ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/katte/thread_pool.rb', line 16 def run procedure = Proc.new { loop { begin @queue.pop.call rescue => e @logger.error(e) end } } @threads ||= @threads_num.times.map { Thread.start &procedure } end |
#stop ⇒ Object
33 34 35 |
# File 'lib/katte/thread_pool.rb', line 33 def stop @threads.each &:kill end |