Google Groups

Re: [Lift] LiftActor future with callback?


David Pollak Apr 16, 2012 10:15 AM
Posted in group: Lift
def longComputation: LAFuture[Result] = {
  val ret: LAFuture[Result] = new LAFuture()

  Schedule(() => {
    // do some stuff that takes a long time and maybe even forks a bunch of threads
    val r: Result = ... the result of the computation
    ret.satisfy(r)
  })

  ret
}

On Mon, Apr 16, 2012 at 9:58 AM, AGYNAMIX Torsten Uhlmann <T.Uh...@agynamix.de> wrote:
David,

thanks for explaining these mechanisms!

May I ask for a short example of "longComputation". I understand this is the way to do it, yet there are some steps I do not fully get:

val result: LAFuture[Result] = new LAFuture[Result] // is this the way to create a new future?
result.satisfy(?) 

Here's my problem: from the source I see satisfy takes a parameter T, not a function. So I simply do not get how and why this parameter T is then forked off into another thread with LAScheduler.execute(…) If it's just a parameter then it's already resolved, right. I think right here is my problem...

Could you explain this step, please?

Thanks,
Torsten.


Am 16.04.2012 um 17:31 schrieb David Pollak:

If you use a foreach (or a for comprehension without a yield) on a future, the computation will be resumed on a separate thread when the future is satisfied:

val result: LAFuture[Result] = longComputation

result.foreach(r => myActor ! r) // returns immediately and when the future is satisfied, the message send it processed.

On Sat, Apr 14, 2012 at 12:48 PM, Diego Medina <di...@fmpwizard.com> wrote:
Hi,

Is there a way to have a callback function send to a LiftActor, along
with a message, so that when the LiftActor is done processing the
message, it will execute my callback?
What I'm looking for is something similar (or exactly) like Twitter's
finagle Future with callback.
https://github.com/twitter/finagle#Future%20Callbacks

As an example:

val request: HttpRequest = new DefaultHttpRequest(HTTP_1_1, GET, "/")
val responseFuture: Future[HttpResponse] = client(request)
responseFuture onSuccess { responseFuture =>
 println(responseFuture)
}

so if responseFuture executes and all goes well, the println will be called.

My real use case is this:

On my snippet I send a message to an actor A, this message has a key
form a key-> value map.
The value is a reference to another LiftActor B, so once A selects the
actor B, it needs to send B another message, so B can do its job.

One limitation I have is that I cannot modify the final message I send
to B, if I could I would just do something like:

case class ComposedMessage(key: String, msg: String)

So, if we had a Future with Callback in Lift I imagine I could do:

case class Message(msg: String)
case class Key(key: String)

val actorResponseFuture= LiftActorA !< Key("123")
actorResponseFuture onSuccess { responseActor =>
 responseActor ! Message("Do this job")
}


So I guess my question now is two fold:

1- Can this be done today?
2- If not, would it be useful to add such a feature?

And just in case it wasn't clear, I'm trying to do it this way so that
the thread my snippet is in does not block while all that processing
is happening.

Thanks

 Diego


--
Diego Medina
Lift/Scala Developer
di...@fmpwizard.com
http://www.fmpwizard.com

--
Lift, the simply functional web framework: http://liftweb.net
Code: http://github.com/lift
Discussion: http://groups.google.com/group/liftweb
Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code



--
Visi.Pro, Cloud Computing for the Rest of Us http://visi.pro
Lift, the simply functional web framework http://liftweb.net



--
Lift, the simply functional web framework: http://liftweb.net
Code: http://github.com/lift
Discussion: http://groups.google.com/group/liftweb
Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code

--
Lift, the simply functional web framework: http://liftweb.net
Code: http://github.com/lift
Discussion: http://groups.google.com/group/liftweb
Stuck? Help us help you: https://www.assembla.com/wiki/show/liftweb/Posting_example_code



--
Visi.Pro, Cloud Computing for the Rest of Us http://visi.pro
Lift, the simply functional web framework http://liftweb.net