Class: AWS::Flow::Core::ExternalFuture

Inherits:
Future
  • Object
show all
Defined in:
lib/aws/flow/future.rb

Overview

Represents the result of an asynchronous computation. Methods are provided to:

  • retrieve the result of the computation, once it is complete (#get).

  • check if the computation is complete (Future#set?)

  • execute a block when computation is complete (Future#on_set)

The result of a Future can only be retrieved when the computation has completed. #get blocks execution, if necessary, until the ExternalFuture is ready.

Unlike Future, #get doesn’t block Fibers. Instead it blocks the current thread by waiting on a ruby ConditionVariable. The condition variable is signalled when the future is set, which allows the thread to continue execution when the result is ready. This lets us use the future outside of an AsyncScope

Instance Method Summary collapse

Methods inherited from Future

#is_flow_future?, #on_set, #set, #set?, #unset

Constructor Details

#initializeExternalFuture

Returns a new instance of ExternalFuture.



160
161
162
163
164
# File 'lib/aws/flow/future.rb', line 160

def initialize
  @conditional = ConditionVariable.new
  @mutex = Mutex.new
  @set = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



184
185
186
187
188
# File 'lib/aws/flow/future.rb', line 184

def method_missing(method, *args, &block)
  @mutex.synchronize do
    super(method, *args, &block)
  end
end

Instance Method Details

#get(timeout = nil) ⇒ Object

Blocks if future is not set. Returns the result of the future once Future#set is true.

Returns:

  • The result of the future.

Raises:

  • CancellationError when the task is cancelled.



174
175
176
177
178
179
180
181
182
# File 'lib/aws/flow/future.rb', line 174

def get(timeout=nil)
  @mutex.synchronize do
    unless @set
      @conditional.wait(@mutex, timeout)
      raise Timeout::Error.new unless @set
    end
  end
  @result
end