Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are Scala Futures chained together with flatMap?

I'm working on using Futures for the first time in Scala and am working through an example of using the flatMap combinator; I've been following this discussion:

http://docs.scala-lang.org/overviews/core/futures.html

Specifically, this example:

val usdQuote = future { connection.getCurrentValue(USD) }
val chfQuote = future { connection.getCurrentValue(CHF) }
val purchase = for {
    usd <- usdQuote
    chf <- chfQuote
      if isProfitable(usd, chf)
} yield connection.buy(amount, chf)

purchase onSuccess {
    case _ => println("Purchased " + amount + " CHF")
}

is translated to this:

val purchase = usdQuote flatMap {
    usd =>
         chfQuote
        .withFilter(chf => isProfitable(usd, chf))
        .map(chf => connection.buy(amount, chf))
}

What I'm having a bit of trouble grasping is how and when this is flatMap executed?

I understand that usdQuote and chfQuote are executed by "some thread" at "some time" and their registered callback functions called, questions are:

a) Are usdQuote and chfQuote executed concurrently? (I'm pretty sure they are).

b) How does flatMap assign the value of the Future useQuote to usd? As in, does it get called when the operation usdQuote completes?

c) What thread is executing the 'flatMap' and 'map' operation (probably more of a follow-on from the last question).

Cheers.

like image 203
JMac Avatar asked Jan 18 '13 07:01

JMac


People also ask

How do Scala Futures work?

Future represents a result of an asynchronous computation that may or may not be available yet. When we create a new Future, Scala spawns a new thread and executes its code. Once the execution is finished, the result of the computation (value or exception) will be assigned to the Future.

How do you handle Future Scala?

Whenever we create a new Future operation, Scala spawns a new thread to run that Future's code, and after completion it executes any provided callbacks. Scala will infer that add has a return type of Future[Int] , and the enclosed code will execute in its own thread when the function is called.

Is Future blocking onComplete?

NOTE: With Future. onComplete() we are no longer blocking for the result from the Future but instead we will receive a callback for either a Success or a Failure.

Is Scala Future blocking?

By default, futures and promises are non-blocking, making use of callbacks instead of typical blocking operations. To simplify the use of callbacks both syntactically and conceptually, Scala provides combinators such as flatMap , foreach , and filter used to compose futures in a non-blocking way.


1 Answers

  • a) When you created them you've already started them executing against the implicit ExecutionContext in scope, so they're potentially running concurrently as it depends on how that is executing them.

  • b) It doesn't really assign the value as such, but the implementation uses the onComplete method to cause the function you've passed to be triggered once a result has been reached. At the current time this should link to that flatMap method I'm referring to: https://github.com/scala/scala/blob/v2.11.2/src/library/scala/concurrent/Future.scala#L246

  • c) Those are running via the ExecutionContext previously mentioned, consider also that if those Future instances can be running on different ExecutionContexts, so parts of the for-comprehension can be running on different thread pools.

like image 146
Sean Parsons Avatar answered Sep 25 '22 11:09

Sean Parsons