Module: Spectre::Async

Defined in:
lib/spectre/async.rb

Constant Summary collapse

@@threads =
{}

Class Method Summary collapse

Class Method Details

.async(name = 'default', &block) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/spectre/async.rb', line 10

def async name='default', &block
  unless @@threads.key? name
    @@threads[name] = []
  end

  @@threads[name] << Thread.new(&block)
end

.await(name = 'default') ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/spectre/async.rb', line 18

def await name='default'
  return unless @@threads.key? name

  threads = @@threads[name].map { |x| x.join() }

  @@threads.delete(name)

  threads.map { |x| x.value }
end