Sha256: eb0293c1f2f8be39cc7fc52af4ea8635a880398c9b1044f5b636086689e3b0d4
Contents?: true
Size: 1.22 KB
Versions: 8
Compression:
Stored size: 1.22 KB
Contents
require "thread" module Bunny # Thread pool that dispatches consumer deliveries. Not supposed to be shared between channels # or threads. class ConsumerWorkPool # # API # attr_reader :size def initialize(size = 1) @size = size @queue = ::Queue.new end def submit(callable = nil, &block) @queue.push(callable || block) end def start @threads = [] @size.times do t = Thread.new(&method(:run_loop)) @threads << t end @started = true end def started? @started end def shutdown @size.times do submit do |*args| throw :terminate end end end def join @threads.each { |t| t.join } end def pause @threads.each { |t| t.stop } end def resume @threads.each { |t| t.run } end def kill @threads.each { |t| t.kill } end protected def run_loop catch(:terminate) do loop do callable = @queue.pop begin callable.call rescue Exception => e # TODO puts e.class.name puts e.message end end end end end end
Version data entries
8 entries across 8 versions & 1 rubygems