Class: Celluloid::Future
- Inherits:
-
Object
- Object
- Celluloid::Future
- Defined in:
- lib/celluloid/future.rb
Overview
Celluloid::Future objects allow methods and blocks to run in the background, their values requested later
Defined Under Namespace
Classes: Result
Instance Attribute Summary (collapse)
-
- (Object) address
readonly
Returns the value of attribute address.
Instance Method Summary (collapse)
-
- (Object) execute(receiver, method, args, block)
Execute the given method in future context.
-
- (Future) initialize(*args, &block)
constructor
Create a future bound to a given receiver, or with a block to compute.
-
- (Boolean) ready?
Check if this future has a value yet.
-
- (Object) signal(value)
(also: #<<)
Signal this future with the given result value.
-
- (Object) value(timeout = nil)
(also: #call)
Obtain the value for this Future.
Constructor Details
- (Future) initialize(*args, &block)
Create a future bound to a given receiver, or with a block to compute
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/celluloid/future.rb', line 10 def initialize(*args, &block) @address = Celluloid.uuid @mutex = Mutex.new @ready = false @result = nil @forwards = nil if block @call = SyncCall.new(self, :call, args) Celluloid.internal_pool.get do begin @call.dispatch(block) rescue # Exceptions in blocks will get raised when the value is retrieved end end else @call = nil end end |
Instance Attribute Details
- (Object) address (readonly)
Returns the value of attribute address
7 8 9 |
# File 'lib/celluloid/future.rb', line 7 def address @address end |
Instance Method Details
- (Object) execute(receiver, method, args, block)
Execute the given method in future context
32 33 34 35 36 37 38 39 |
# File 'lib/celluloid/future.rb', line 32 def execute(receiver, method, args, block) @mutex.synchronize do raise "already calling" if @call @call = SyncCall.new(self, method, args, block) end receiver << @call end |
- (Boolean) ready?
Check if this future has a value yet
42 43 44 |
# File 'lib/celluloid/future.rb', line 42 def ready? @ready end |
- (Object) signal(value) Also known as: <<
Signal this future with the given result value
86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/celluloid/future.rb', line 86 def signal(value) result = Result.new(value, self) @mutex.synchronize do raise "the future has already happened!" if @ready if @forwards @forwards.is_a?(Array) ? @forwards.each { |f| f << result } : @forwards << result end @result = result @ready = true end end |
- (Object) value(timeout = nil) Also known as: call
Obtain the value for this Future
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/celluloid/future.rb', line 47 def value(timeout = nil) ready = result = nil begin @mutex.lock raise "no call requested" unless @call if @ready ready = true result = @result else case @forwards when Array @forwards << Celluloid.mailbox when NilClass @forwards = Celluloid.mailbox else @forwards = [@forwards, Celluloid.mailbox] end end ensure @mutex.unlock end unless ready result = Celluloid.receive(timeout) do |msg| msg.is_a?(Future::Result) && msg.future == self end end if result result.value else raise TimeoutError, "Timed out" end end |