class ThreadKit::Pool
Attributes
jobs[R]
size[R]
workers[R]
Public Class Methods
new(size)
click to toggle source
# File lib/thread_kit/pool.rb, line 5 def initialize(size) @size = size @jobs = Queue.new @workers = [] fill_the_pool at_exit { shutdown } end
Public Instance Methods
add_worker()
click to toggle source
create a worker and start it
# File lib/thread_kit/pool.rb, line 38 def add_worker workers << Thread.new { do_work } end
do_work()
click to toggle source
perform jobs until the exit signal is thrown
# File lib/thread_kit/pool.rb, line 48 def do_work catch(:exit) do loop { perform_job } end end
exit_worker()
click to toggle source
cause one worker to exit
# File lib/thread_kit/pool.rb, line 43 def exit_worker schedule { throw :exit } end
fill_the_pool()
click to toggle source
populate the worker pool
# File lib/thread_kit/pool.rb, line 33 def fill_the_pool size.times { add_worker } end
perform_job()
click to toggle source
dequeue and perform a single job
# File lib/thread_kit/pool.rb, line 55 def perform_job job, args = @jobs.pop job.call(*args) end
schedule(*args, &block)
click to toggle source
schedule a job to be performed
# File lib/thread_kit/pool.rb, line 14 def schedule(*args, &block) jobs << [block, args] end
shutdown()
click to toggle source
bail after finishing remaining jobs
# File lib/thread_kit/pool.rb, line 19 def shutdown size.times { exit_worker } workers.map(&:join) end
shutdown!()
click to toggle source
bail without finishing remaining jobs
# File lib/thread_kit/pool.rb, line 25 def shutdown! jobs.clear shutdown end