Class: Buckler::ThreadDispatch

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/buckler/thread_dispatch.rb

Instance Method Summary collapse

Methods included from Logging

#alert, #log, #verbose

Constructor Details

#initializeThreadDispatch

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

Returns:

  • (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_waitObject



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_countObject



20
21
22
# File 'lib/buckler/thread_dispatch.rb', line 20

def running_thread_count
  @thread_pool.select{|s| s.status == "run"}.count
end