Class: PromisePool::ThreadPool
- Inherits:
-
Object
- Object
- PromisePool::ThreadPool
- Defined in:
- lib/promise_pool/thread_pool.rb
Instance Attribute Summary collapse
-
#idle_time ⇒ Object
Returns the value of attribute idle_time.
-
#max_size ⇒ Object
Returns the value of attribute max_size.
-
#workers ⇒ Object
readonly
Returns the value of attribute workers.
Instance Method Summary collapse
- #defer(promise_mutex, &job) ⇒ Object
-
#initialize(max_size, idle_time = 60) ⇒ ThreadPool
constructor
A new instance of ThreadPool.
- #queue_size ⇒ Object
-
#shutdown ⇒ Object
Block on shutting down, and should not add more jobs while shutting down.
- #size ⇒ Object
- #trim(force = false) ⇒ Object
Constructor Details
#initialize(max_size, idle_time = 60) ⇒ ThreadPool
Returns a new instance of ThreadPool.
14 15 16 17 18 19 20 21 |
# File 'lib/promise_pool/thread_pool.rb', line 14 def initialize max_size, idle_time=60 @max_size = max_size @idle_time = idle_time @queue = Queue.new @mutex = Mutex.new @workers = [] @waiting = 0 end |
Instance Attribute Details
#idle_time ⇒ Object
Returns the value of attribute idle_time.
12 13 14 |
# File 'lib/promise_pool/thread_pool.rb', line 12 def idle_time @idle_time end |
#max_size ⇒ Object
Returns the value of attribute max_size.
12 13 14 |
# File 'lib/promise_pool/thread_pool.rb', line 12 def max_size @max_size end |
#workers ⇒ Object (readonly)
Returns the value of attribute workers.
11 12 13 |
# File 'lib/promise_pool/thread_pool.rb', line 11 def workers @workers end |
Instance Method Details
#defer(promise_mutex, &job) ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'lib/promise_pool/thread_pool.rb', line 31 def defer promise_mutex, &job mutex.synchronize do task = Task.new(job, promise_mutex) queue << task spawn_worker if waiting < queue_size && workers.size < max_size task end end |
#queue_size ⇒ Object
27 28 29 |
# File 'lib/promise_pool/thread_pool.rb', line 27 def queue_size queue.size end |
#shutdown ⇒ Object
Block on shutting down, and should not add more jobs while shutting down
47 48 49 50 51 |
# File 'lib/promise_pool/thread_pool.rb', line 47 def shutdown workers.size.times{ trim(true) } workers.first.join && trim(true) until workers.empty? mutex.synchronize{ queue.clear } end |
#size ⇒ Object
23 24 25 |
# File 'lib/promise_pool/thread_pool.rb', line 23 def size workers.size end |
#trim(force = false) ⇒ Object
40 41 42 43 44 |
# File 'lib/promise_pool/thread_pool.rb', line 40 def trim force=false mutex.synchronize do queue << lambda{ |_| false } if force || waiting > 0 end end |