Class: Buckler::ThreadDispatch
- Inherits:
-
Object
- Object
- Buckler::ThreadDispatch
- Includes:
- Logging
- Defined in:
- lib/buckler/thread_dispatch.rb
Instance Method Summary collapse
- #any_running_threads? ⇒ Boolean
-
#initialize ⇒ ThreadDispatch
constructor
A new instance of ThreadDispatch.
- #perform_and_wait ⇒ Object
- #queue(λ) ⇒ Object
- #running_thread_count ⇒ Object
Methods included from Logging
Constructor Details
#initialize ⇒ ThreadDispatch
Returns a new instance of ThreadDispatch.
6 7 8 9 10 |
# File 'lib/buckler/thread_dispatch.rb', line 6 def initialize @lambda_pool = [] @thread_pool = [] @max_threads = Etc.nprocessors * 2 # Twice the number of CPU cores available to Ruby end |
Instance Method Details
#any_running_threads? ⇒ Boolean
16 17 18 |
# File 'lib/buckler/thread_dispatch.rb', line 16 def any_running_threads? @thread_pool.any?{|s| s.status == "run"} end |
#perform_and_wait ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/buckler/thread_dispatch.rb', line 24 def perform_and_wait Thread.abort_on_exception = true start_time = Time.now @lambda_pool.each do |λ| while running_thread_count >= @max_threads verbose "Sleeping due to worker limit. #{running_thread_count} currently running." sleep 0.2 end @thread_pool << Thread.new(&λ) end verbose "All workers spawned, waiting for workers to finish" while any_running_threads? do sleep 0.2 end return (Time.now - start_time).round(2) end |
#queue(λ) ⇒ Object
12 13 14 |
# File 'lib/buckler/thread_dispatch.rb', line 12 def queue(λ) @lambda_pool << λ end |
#running_thread_count ⇒ Object
20 21 22 |
# File 'lib/buckler/thread_dispatch.rb', line 20 def running_thread_count @thread_pool.select{|s| s.status == "run"}.count end |