1

Given an instance of a Celluloid Actor, you can use future to execute an Actor method asynchronously and at some later point in time use the Future's value method to obtain the result of the Actor method (blocking if necessary).

Let's say that I have two separate components in a system that both want to use the same Actor method, perhaps a very expensive database query. If both of these components individually called actor.future.expensive_query, then the query would be executed twice and each caller would get their own separate Future object back to retrieve the results. Furthermore, the two queries would be executed serially, not concurrently. What if, instead, I wanted to have the second call to actor.future.expensive_query to just get a reference to the Future object created by the first caller? Is such a thing possible with Celluloid?

Jason Voegele
  • 1,918
  • 1
  • 19
  • 18

1 Answers1

0

Yes, but it's also just possible with plain Ruby, as I will show you.

For example, use this custom accessor:

def expensive_query!(*args)
  @expensive_query ||= future.expensive_query(*args)
  if @expensive_query.is_a? Celluloid::Future
    @expensive_query = @expensive_query.value
  end
  @expensive_query
end

Keep in mind, if you want to run the query again, you will need to externally do this somewhere... otherwise you'll be pulling the result of an already run query ( stored in @expensive_query ):

@expensive_query = nil

Perhaps in reset_expensive_query! for example.

digitalextremist
  • 5,952
  • 3
  • 43
  • 62