Class: SuchPool

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

Overview

Such Pool is the simplest implementation as possible of a Thread pool in ruby. Making possible to schedule and run your operation on background

Constant Summary collapse

VERSION =
'0.1.0'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pool_size: 5) ⇒ SuchPool

Returns a new instance of SuchPool.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/such_pool.rb', line 9

def initialize(pool_size: 5)
  @pool_size = pool_size
  @queue = Queue.new
  @pool_size.times do
    Thread.new do
      loop do
        lambd = @queue.pop
        lambd.call
      end
    end
  end
end

Instance Attribute Details

#pool_sizeObject (readonly)

Returns the value of attribute pool_size.



7
8
9
# File 'lib/such_pool.rb', line 7

def pool_size
  @pool_size
end

Instance Method Details

#run_background(&lambd) ⇒ Object



22
23
24
# File 'lib/such_pool.rb', line 22

def run_background &lambd
  @queue << lambd
end