Class: ThreadKit::Pool

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ Pool

Returns a new instance of Pool.



5
6
7
8
9
10
11
# File 'lib/thread_kit/pool.rb', line 5

def initialize(size)
  @size = size
  @jobs = Queue.new
  @workers = []
  fill_the_pool
  at_exit { shutdown }
end

Instance Attribute Details

#jobsObject (readonly)

Returns the value of attribute jobs.



3
4
5
# File 'lib/thread_kit/pool.rb', line 3

def jobs
  @jobs
end

#sizeObject (readonly)

Returns the value of attribute size.



3
4
5
# File 'lib/thread_kit/pool.rb', line 3

def size
  @size
end

#workersObject (readonly)

Returns the value of attribute workers.



3
4
5
# File 'lib/thread_kit/pool.rb', line 3

def workers
  @workers
end

Instance Method Details

#schedule(*args, &block) ⇒ Object

schedule a job to be performed



14
15
16
# File 'lib/thread_kit/pool.rb', line 14

def schedule(*args, &block)
  jobs << [block, args]
end

#shutdownObject

bail after finishing remaining jobs



19
20
21
22
# File 'lib/thread_kit/pool.rb', line 19

def shutdown
  size.times { exit_worker }
  workers.map(&:join)
end

#shutdown!Object

bail without finishing remaining jobs



25
26
27
28
# File 'lib/thread_kit/pool.rb', line 25

def shutdown!
  jobs.clear
  shutdown
end