Skip to content

Commit

Permalink
more future cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
careo committed Feb 5, 2009
1 parent eed7f96 commit ea615cc
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions future_rpc.rb
Expand Up @@ -11,6 +11,7 @@ class Future

def initialize &blk
@f = Fiber.new {

@d = blk.call
if @d.kind_of? EM::DefaultDeferrable
@d.callback { |val|
Expand All @@ -19,11 +20,13 @@ def initialize &blk
@d.errback { |val|
@f.resume val
}

else
@result = d
end
@result = Fiber.yield
}.resume
}
@f.resume
self
end

Expand Down Expand Up @@ -58,26 +61,19 @@ def rpc val, &blk
EventMachine.run {
start_time = Time.now

EM.next_tick {
# Need to wrap this in a fiber so I can block the whole thing
# in case the value of a future is demanded.
f = Fiber.new {
start = Time.now
one = Future.new { rpc(1) }
two = Future.new { rpc(2) }

puts "at #{Time.now - start}, about to use futures"
result = one + two
puts "finished with futures at #{Time.now - start}"
puts result
}

EM.next_tick {
f.resume
}

}
# Need to wrap this in a fiber so I can block the whole thing
# in case the value of a future is demanded.
Fiber.new {
start = Time.now
one = Future.new { rpc(1) }
two = Future.new { rpc(2) }

puts "at #{Time.now - start}, about to use futures"
result = one + two
puts "finished with futures at #{Time.now - start}"
puts result
}.resume

# ensure we eventually do quit
EventMachine.add_timer(5) {
puts "Total Runtime: #{Time.now - start_time}"
Expand Down

0 comments on commit ea615cc

Please sign in to comment.