class AWS::Flow::Core::ExternalFuture
Represents the result of an asynchronous computation. Methods are provided to:
-
retrieve the result of the computation, once it is complete ({ExternalFuture#get}).
-
check if the computation is complete ({ExternalFuture#set?})
-
execute a block when computation is complete ({ExternalFuture#on_set})
The result of a Future
can only be retrieved when the computation has completed. {ExternalFuture#get} blocks execution, if necessary, until the ExternalFuture
is ready.
Unlike {Future}, {ExternalFuture#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}
Public Class Methods
# File lib/aws/flow/future.rb, line 160 def initialize @conditional = ConditionVariable.new @mutex = Mutex.new @set = false end
Public Instance Methods
Blocks if future is not set. Returns the result of the future once {#set} is true.
@return
The result of the future.
@raise CancellationError
when the task is cancelled.
# 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
# File lib/aws/flow/future.rb, line 184 def method_missing(method, *args, &block) @mutex.synchronize do super(method, *args, &block) end end