Method: Async::Reactor.run

Defined in:
lib/async/reactor.rb

.run(*args, &block) ⇒ Object

The preferred method to invoke asynchronous behavior at the top level.

  • When invoked within an existing reactor task, it will run the given block asynchronously. Will return the task once it has been scheduled.
  • When invoked at the top level, will create and run a reactor, and invoke the block as an asynchronous task. Will block until the reactor finishes running.


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/async/reactor.rb', line 45

def self.run(*args, &block)
  if current = Task.current?
    reactor = current.reactor
    
    reactor.async(*args, &block)
  else
    reactor = self.new
    
    begin
      reactor.run(*args, &block)
    ensure
      reactor.close
    end
    
    return reactor
  end
end