Class: ThreadPuddle
- Inherits:
-
Object
- Object
- ThreadPuddle
- Defined in:
- lib/thread_puddle.rb
Instance Attribute Summary collapse
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
-
#initialize(size = 24) ⇒ ThreadPuddle
constructor
A new instance of ThreadPuddle.
- #shutdown(timeout = nil) ⇒ Object
- #status ⇒ Object
- #submit(*args, &block) ⇒ Object
Constructor Details
#initialize(size = 24) ⇒ ThreadPuddle
Returns a new instance of ThreadPuddle.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/thread_puddle.rb', line 6 def initialize size = 24 @size = size @queue = Queue.new @pool = size.times.map do Thread.new do catch :exit do loop do job, args = queue.deq job.call *args end end end end end |
Instance Attribute Details
#size ⇒ Object (readonly)
Returns the value of attribute size.
4 5 6 |
# File 'lib/thread_puddle.rb', line 4 def size @size end |
Instance Method Details
#shutdown(timeout = nil) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/thread_puddle.rb', line 25 def shutdown timeout = nil size.times do submit do throw :exit end end if timeout shutdown_with_timeout timeout else pool.each &:join end queue.clear size = 0 end |
#status ⇒ Object
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/thread_puddle.rb', line 42 def status { :size => size, :threads => pool.map(&:status), :queue => { :size => queue.size, :consumers => queue.num_waiting, }, } end |
#submit(*args, &block) ⇒ Object
21 22 23 |
# File 'lib/thread_puddle.rb', line 21 def submit *args, &block queue.enq [block, args] end |