class Plum::Rack::ThreadPool

Public Class Methods

new(size) click to toggle source
# File lib/plum/rack/thread_pool.rb, line 4
def initialize(size)
  @workers = Set.new
  @jobs = Queue.new

  size.times { |i|
    spawn_worker
  }
end

Public Instance Methods

acquire(tag = nil, err = nil, &blk) click to toggle source

returns cancel token

# File lib/plum/rack/thread_pool.rb, line 14
def acquire(tag = nil, err = nil, &blk)
  @jobs << [blk, err]
end

Private Instance Methods

spawn_worker() click to toggle source
# File lib/plum/rack/thread_pool.rb, line 19
def spawn_worker
  t = Thread.new {
    while true
      job, err = @jobs.pop
      begin
        job.call
      rescue => e
        err << e if err
      end
    end
  }
  @workers << t
end