Class: GHArchive::ThreadPool
- Inherits:
-
Object
- Object
- GHArchive::ThreadPool
- Defined in:
- lib/gh-archive/core.rb
Instance Method Summary collapse
- #alive? ⇒ Boolean
- #enqueued ⇒ Object
-
#initialize(size) ⇒ ThreadPool
constructor
A new instance of ThreadPool.
- #process(*args, &block) ⇒ Object
- #shutdown ⇒ Object
- #shutdown! ⇒ Object
- #shutdown? ⇒ Boolean
- #wait ⇒ Object
Constructor Details
#initialize(size) ⇒ ThreadPool
Returns a new instance of ThreadPool.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/gh-archive/core.rb', line 12 def initialize(size) @size = size @threads = [] @queue = [] @mutex = Mutex.new @consumer_thread = Thread.start do while !@shutdown || @threads.size > 0 || @queue.size > 0 sleep 0.1 if @queue.size == 0 || @threads.size == @size @threads.delete_if { |t| !t.alive? } if @threads.size < @size && @queue.size > 0 @mutex.synchronize do args, job = @queue.shift @threads << Thread.start(*args, &job) end end end end end |
Instance Method Details
#alive? ⇒ Boolean
63 64 65 |
# File 'lib/gh-archive/core.rb', line 63 def alive? @consumer_thread.alive? end |
#enqueued ⇒ Object
55 56 57 |
# File 'lib/gh-archive/core.rb', line 55 def enqueued return @queue.size end |
#process(*args, &block) ⇒ Object
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/gh-archive/core.rb', line 33 def process(*args, &block) raise "Block expected" unless block_given? raise "Can not add jobs while shutting down" if @shutdown @mutex.synchronize do @queue << [args, block] end return self.enqueued end |
#shutdown ⇒ Object
44 45 46 |
# File 'lib/gh-archive/core.rb', line 44 def shutdown @shutdown = true end |
#shutdown! ⇒ Object
48 49 50 51 52 53 |
# File 'lib/gh-archive/core.rb', line 48 def shutdown! self.shutdown @mutex.synchronize do @queue.clear end end |
#shutdown? ⇒ Boolean
59 60 61 |
# File 'lib/gh-archive/core.rb', line 59 def shutdown? @shutdown end |
#wait ⇒ Object
67 68 69 70 71 |
# File 'lib/gh-archive/core.rb', line 67 def wait while alive? sleep 0.1 end end |