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

Inherits:
Future
  • Object
show all
Defined in:
aws-flow-ruby/aws-flow/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 (#set?)

  • execute a block when computation is complete (#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

#on_set, #set?

Constructor Details

- (ExternalFuture) initialize

Returns a new instance of ExternalFuture



165
166
167
168
169
# File 'aws-flow-ruby/aws-flow/lib/aws/flow/future.rb', line 165

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(method, *args, &block)



189
190
191
192
193
# File 'aws-flow-ruby/aws-flow/lib/aws/flow/future.rb', line 189

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

Instance Method Details

- (Object) get(timeout = nil)

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.



179
180
181
182
183
184
185
186
187
# File 'aws-flow-ruby/aws-flow/lib/aws/flow/future.rb', line 179

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