futures, actors, and streamsstg-tud.github.io/ctbd/2016/ctbd_09_futures_actors.pdf · futures,...
TRANSCRIPT
![Page 1: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/1.jpg)
Concepts and Technologiesfor
Distributed Systems and Big Data Processing
Philipp HallerKTH Royal Institute of Technology
Sweden
TU Darmstadt, Germany, 24 May 2016
Futures, Actors, and Streams
![Page 2: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/2.jpg)
Philipp Haller
Programming a Concurrent World
• How to compose programs handling
• asynchronous events?
• streams of asynchronous events?
• distributed events?
➟ Programming abstractions for concurrency!
2
![Page 3: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/3.jpg)
Philipp Haller
Overview
• Futures, promises
• Async/await
• Actors
3
![Page 4: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/4.jpg)
Philipp Haller
Why a Growable Language for Concurrency?
4
![Page 5: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/5.jpg)
Philipp Haller
Why a Growable Language for Concurrency?
• Concurrency not a solved problem ➟ development of new programming models
4
![Page 6: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/6.jpg)
Philipp Haller
Why a Growable Language for Concurrency?
• Concurrency not a solved problem ➟ development of new programming models
4
• Futures, promises
![Page 7: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/7.jpg)
Philipp Haller
Why a Growable Language for Concurrency?
• Concurrency not a solved problem ➟ development of new programming models
4
• Futures, promises
• async/await
![Page 8: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/8.jpg)
Philipp Haller
Why a Growable Language for Concurrency?
• Concurrency not a solved problem ➟ development of new programming models
4
• Futures, promises
• async/await
• STM
![Page 9: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/9.jpg)
Philipp Haller
Why a Growable Language for Concurrency?
• Concurrency not a solved problem ➟ development of new programming models
4
• Futures, promises
• async/await
• STM
• Agents
![Page 10: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/10.jpg)
Philipp Haller
Why a Growable Language for Concurrency?
• Concurrency not a solved problem ➟ development of new programming models
4
• Futures, promises
• async/await
• STM
• Agents
• Actors
![Page 11: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/11.jpg)
Philipp Haller
Why a Growable Language for Concurrency?
• Concurrency not a solved problem ➟ development of new programming models
4
• Futures, promises
• async/await
• STM
• Agents
• Actors
• Join-calculus
![Page 12: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/12.jpg)
Philipp Haller
Why a Growable Language for Concurrency?
• Concurrency not a solved problem ➟ development of new programming models
4
• Futures, promises
• async/await
• STM
• Agents
• Actors
• Join-calculus
• Reactive streams
![Page 13: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/13.jpg)
Philipp Haller
Why a Growable Language for Concurrency?
• Concurrency not a solved problem ➟ development of new programming models
4
• Futures, promises
• async/await
• STM
• Agents
• Actors
• Join-calculus
• Reactive streams
• CSP
![Page 14: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/14.jpg)
Philipp Haller
Why a Growable Language for Concurrency?
• Concurrency not a solved problem ➟ development of new programming models
4
• Futures, promises
• async/await
• STM
• Agents
• Actors
• Join-calculus
• Reactive streams
• CSP
• Async CML
![Page 15: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/15.jpg)
Philipp Haller
Why a Growable Language for Concurrency?
• Concurrency not a solved problem ➟ development of new programming models
4
• Futures, promises
• async/await
• STM
• Agents
• Actors
• Join-calculus
• Reactive streams
• CSP
• Async CML
• …
![Page 16: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/16.jpg)
Philipp Haller
Why a Growable Language for Concurrency?
• Concurrency not a solved problem ➟ development of new programming models
4
• Futures, promises
• async/await
• STM
• Agents
• Actors
• Join-calculus
• Reactive streams
• CSP
• Async CML
• …Which one is going to “win”?
![Page 17: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/17.jpg)
Philipp Haller
Background
5
![Page 18: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/18.jpg)
Philipp Haller
Background
• Authored or co-authored:
• Scala Actors (2006)
• Scala futures and promises (2011/2012)
• Scala Async (2013)
5
![Page 19: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/19.jpg)
Philipp Haller
Background
• Authored or co-authored:
• Scala Actors (2006)
• Scala futures and promises (2011/2012)
• Scala Async (2013)
• Contributed to Akka (Typesafe/Lightbend)
5
![Page 20: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/20.jpg)
Philipp Haller
Background
• Authored or co-authored:
• Scala Actors (2006)
• Scala futures and promises (2011/2012)
• Scala Async (2013)
• Contributed to Akka (Typesafe/Lightbend)
• Akka.js project (2014)
5
![Page 21: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/21.jpg)
Philipp Haller
Background
• Authored or co-authored:
• Scala Actors (2006)
• Scala futures and promises (2011/2012)
• Scala Async (2013)
• Contributed to Akka (Typesafe/Lightbend)
• Akka.js project (2014)
5
Other proposals and research projects:
• Scala Joins (2008)
• FlowPools (2012)
• Spores (safer closures)
• Capabilities and uniqueness
• …
![Page 22: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/22.jpg)
Philipp Haller
Scala Primer
6
![Page 23: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/23.jpg)
Philipp Haller
Scala Primer
• Local variables: val x = fun(arg) // type inference
6
![Page 24: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/24.jpg)
Philipp Haller
Scala Primer
• Local variables: val x = fun(arg) // type inference
• Functions:
6
![Page 25: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/25.jpg)
Philipp Haller
Scala Primer
• Local variables: val x = fun(arg) // type inference
• Functions:
• { param => fun(param) }
6
![Page 26: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/26.jpg)
Philipp Haller
Scala Primer
• Local variables: val x = fun(arg) // type inference
• Functions:
• { param => fun(param) }
• (param: T) => fun(param)
6
![Page 27: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/27.jpg)
Philipp Haller
Scala Primer
• Local variables: val x = fun(arg) // type inference
• Functions:
• { param => fun(param) }
• (param: T) => fun(param)
• Function type: T => S or (T, S) => U
6
![Page 28: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/28.jpg)
Philipp Haller
Scala Primer
• Local variables: val x = fun(arg) // type inference
• Functions:
• { param => fun(param) }
• (param: T) => fun(param)
• Function type: T => S or (T, S) => U
• Methods: def meth(x: T, y: S): R = { .. }
6
![Page 29: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/29.jpg)
Philipp Haller
Scala Primer
• Local variables: val x = fun(arg) // type inference
• Functions:
• { param => fun(param) }
• (param: T) => fun(param)
• Function type: T => S or (T, S) => U
• Methods: def meth(x: T, y: S): R = { .. }
• Classes: class C extends D { .. }
6
![Page 30: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/30.jpg)
Philipp Haller
Scala Primer (2)
7
![Page 31: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/31.jpg)
Philipp Haller
Scala Primer (2)
7
• Generics:
![Page 32: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/32.jpg)
Philipp Haller
Scala Primer (2)
7
• Generics:
• class C[T] { var fld: T = _ ; .. }
![Page 33: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/33.jpg)
Philipp Haller
Scala Primer (2)
7
• Generics:
• class C[T] { var fld: T = _ ; .. }
• def convert[T](obj: T): Result = ..
![Page 34: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/34.jpg)
Philipp Haller
Scala Primer (2)
7
• Generics:
• class C[T] { var fld: T = _ ; .. }
• def convert[T](obj: T): Result = ..
• Pattern matching:
![Page 35: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/35.jpg)
Philipp Haller
Scala Primer (2)
7
• Generics:
• class C[T] { var fld: T = _ ; .. }
• def convert[T](obj: T): Result = ..
• Pattern matching:
• case class Person(name: String, age: Int)
![Page 36: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/36.jpg)
Philipp Haller
Scala Primer (2)
7
• Generics:
• class C[T] { var fld: T = _ ; .. }
• def convert[T](obj: T): Result = ..
• Pattern matching:
• case class Person(name: String, age: Int)
• val isAdult = p match { case Person(_, a) => a >= 18 case Alien(_, _) => false }
![Page 37: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/37.jpg)
Philipp Haller
Example
• Common task:
• Convert object to JSON
• Send HTTP request containing JSON
8
![Page 38: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/38.jpg)
Philipp Haller
Example
• Common task:
• Convert object to JSON
• Send HTTP request containing JSON
8
import scala.util.parsing.json._
def convert[T](obj: T): Future[JSONType] def sendReq(json: JSONType): Future[JSONType]
![Page 39: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/39.jpg)
Philipp Haller
Example
• Common task:
• Convert object to JSON
• Send HTTP request containing JSON
8
import scala.util.parsing.json._
def convert[T](obj: T): Future[JSONType] def sendReq(json: JSONType): Future[JSONType]
![Page 40: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/40.jpg)
Philipp Haller
Example
• Common task:
• Convert object to JSON
• Send HTTP request containing JSON
8
import scala.util.parsing.json._
def convert[T](obj: T): Future[JSONType] def sendReq(json: JSONType): Future[JSONType]
Ousterhout et al. Making sense of performance in data analytics frameworks. NSDI ’15
![Page 41: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/41.jpg)
Philipp Haller
Callbacks
• How to respond to asynchronous completion event?
➟ Register callback
9
![Page 42: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/42.jpg)
Philipp Haller
Callbacks
• How to respond to asynchronous completion event?
➟ Register callback
9
val person = Person(“Tim”, 25)
val fut: Future[JSONType] = convert(person)
fut.foreach { json => val resp: Future[JSONType] = sendReq(json) .. }
![Page 43: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/43.jpg)
Philipp Haller
Exceptions
• Serialization to JSON may fail at runtime
• Closure passed to foreach not executed in this case
• How to handle asynchronous exceptions?
10
![Page 44: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/44.jpg)
Philipp Haller
Exceptions
• Serialization to JSON may fail at runtime
• Closure passed to foreach not executed in this case
• How to handle asynchronous exceptions?
10
val fut: Future[JSONType] = convert(person)
fut.onComplete { case Success(json) => val resp: Future[JSONType] = sendReq(json) case Failure(e) => e.printStackTrace() }
![Page 45: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/45.jpg)
Philipp Haller
Partial Functions
11
{ case Success(json) => .. case Failure(e) => .. }
![Page 46: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/46.jpg)
Philipp Haller
Partial Functions
11
{ case Success(json) => .. case Failure(e) => .. }
… creates an instance of PartialFunction[T, R]:
![Page 47: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/47.jpg)
Philipp Haller
Partial Functions
11
{ case Success(json) => .. case Failure(e) => .. }
… creates an instance of PartialFunction[T, R]:
val pf: PartialFunction[Try[JSONType], Any] = { case Success(json) => .. case Failure(e) => .. }
![Page 48: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/48.jpg)
Philipp Haller
Type of Partial Functions
• Partial functions have a type PartialFunction[A, B]
• PartialFunction[A, B] is a subtype of Function1[A, B]
12
![Page 49: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/49.jpg)
Philipp Haller
Type of Partial Functions
• Partial functions have a type PartialFunction[A, B]
• PartialFunction[A, B] is a subtype of Function1[A, B]
12
abstract class Function1[A, B] { def apply(x: A): B .. }
abstract class PartialFunction[A, B] extends Function1[A, B] { def isDefinedAt(x: A): Boolean def orElse[A1 <: A, B1 >: B] (that: PartialFunction[A1, B1]): PartialFunction[A1, B1] .. }
![Page 50: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/50.jpg)
Philipp Haller
Type of Partial Functions
• Partial functions have a type PartialFunction[A, B]
• PartialFunction[A, B] is a subtype of Function1[A, B]
12
abstract class Function1[A, B] { def apply(x: A): B .. }
abstract class PartialFunction[A, B] extends Function1[A, B] { def isDefinedAt(x: A): Boolean def orElse[A1 <: A, B1 >: B] (that: PartialFunction[A1, B1]): PartialFunction[A1, B1] .. }
Simplified!
![Page 51: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/51.jpg)
Philipp Haller
Success and Failure
13
package scala.util
abstract class Try[+T]
case class Success[+T](v: T) extends Try[T]
case class Failure[+T](e: Throwable) extends Try[T]
![Page 52: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/52.jpg)
Philipp Haller
Nested Exceptions
➟ Exception handling tedious and not compositional:
14
![Page 53: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/53.jpg)
Philipp Haller
Nested Exceptions
➟ Exception handling tedious and not compositional:
14
val fut: Future[JSONType] = convert(person)
fut.onComplete { case Success(json) => val resp: Future[JSONType] = sendReq(json) resp.onComplete { case Success(jsonResp) => .. // happy path case Failure(e1) => e1.printStackTrace(); ??? } case Failure(e2) => e2.printStackTrace(); ??? }
![Page 54: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/54.jpg)
Philipp Haller
Failed Futures
15
![Page 55: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/55.jpg)
Philipp Haller
Failed Futures
• Future[T] is completed with Try[T], i.e., with success or failure
15
![Page 56: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/56.jpg)
Philipp Haller
Failed Futures
• Future[T] is completed with Try[T], i.e., with success or failure
• Combinators enable compositional failure handling
15
![Page 57: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/57.jpg)
Philipp Haller
Failed Futures
• Future[T] is completed with Try[T], i.e., with success or failure
• Combinators enable compositional failure handling
• Example:
15
![Page 58: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/58.jpg)
Philipp Haller
Failed Futures
• Future[T] is completed with Try[T], i.e., with success or failure
• Combinators enable compositional failure handling
• Example:
15
val resp: Future[JSONType] = sendReq(json) val processed = resp.map { jsonResp => .. // happy path }
![Page 59: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/59.jpg)
Philipp Haller
Failed Futures
• Future[T] is completed with Try[T], i.e., with success or failure
• Combinators enable compositional failure handling
• Example:
15
val resp: Future[JSONType] = sendReq(json) val processed = resp.map { jsonResp => .. // happy path }
Encapsulates failure
![Page 60: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/60.jpg)
Philipp Haller
Map Combinator
16
![Page 61: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/61.jpg)
Philipp Haller
Map Combinator• Creates a new future by applying a function to the
successful result of the receiver future
16
![Page 62: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/62.jpg)
Philipp Haller
Map Combinator• Creates a new future by applying a function to the
successful result of the receiver future
• If the function application results in an uncaught exception e then the new future is completed with e
16
![Page 63: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/63.jpg)
Philipp Haller
Map Combinator• Creates a new future by applying a function to the
successful result of the receiver future
• If the function application results in an uncaught exception e then the new future is completed with e
• If the receiver future is completed with an exception e then the new, pending future is also completed with e
16
![Page 64: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/64.jpg)
Philipp Haller
Map Combinator• Creates a new future by applying a function to the
successful result of the receiver future
• If the function application results in an uncaught exception e then the new future is completed with e
• If the receiver future is completed with an exception e then the new, pending future is also completed with e
16
abstract class Future[+T] extends Awaitable[T] {
def map[S](f: T => S)(implicit ..): Future[S]
// .. }
![Page 65: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/65.jpg)
Philipp Haller
Future Composition
17
val fut: Future[JSONType] = convert(person)
val processed = fut.map { json => val resp: Future[JSONType] = sendReq(json) resp.map { jsonResp => .. // happy path } }
![Page 66: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/66.jpg)
Philipp Haller
Future Composition
17
val fut: Future[JSONType] = convert(person)
val processed = fut.map { json => val resp: Future[JSONType] = sendReq(json) resp.map { jsonResp => .. // happy path } }
Encapsulates all failures
![Page 67: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/67.jpg)
Philipp Haller
Future Composition
17
val fut: Future[JSONType] = convert(person)
val processed = fut.map { json => val resp: Future[JSONType] = sendReq(json) resp.map { jsonResp => .. // happy path } }
Encapsulates all failures
Problem: processed has type
Future[Future[T]]
![Page 68: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/68.jpg)
Philipp Haller
Future Pipelining
18
val fut: Future[JSONType] = convert(person)
val processed = fut.flatMap { json => val resp: Future[JSONType] = sendReq(json) resp.map { jsonResp => .. // happy path } }
![Page 69: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/69.jpg)
Philipp Haller
Future Pipelining
18
val fut: Future[JSONType] = convert(person)
val processed = fut.flatMap { json => val resp: Future[JSONType] = sendReq(json) resp.map { jsonResp => .. // happy path } }
![Page 70: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/70.jpg)
Philipp Haller
Future Pipelining
Future pipelining: the result of the inner future (result of
map) determines the result of the outer future (processed)
18
val fut: Future[JSONType] = convert(person)
val processed = fut.flatMap { json => val resp: Future[JSONType] = sendReq(json) resp.map { jsonResp => .. // happy path } }
![Page 71: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/71.jpg)
Philipp Haller
FlatMap Combinator
19
![Page 72: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/72.jpg)
Philipp Haller
FlatMap Combinator• Creates a new future by applying a function to the
successful result of the receiver future
19
![Page 73: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/73.jpg)
Philipp Haller
FlatMap Combinator• Creates a new future by applying a function to the
successful result of the receiver future
• The future result of the function application determines the result of the new future
19
![Page 74: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/74.jpg)
Philipp Haller
FlatMap Combinator• Creates a new future by applying a function to the
successful result of the receiver future
• The future result of the function application determines the result of the new future
• If the function application results in an uncaught exception e then the new future is completed with e
19
![Page 75: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/75.jpg)
Philipp Haller
FlatMap Combinator• Creates a new future by applying a function to the
successful result of the receiver future
• The future result of the function application determines the result of the new future
• If the function application results in an uncaught exception e then the new future is completed with e
• If the receiver future is completed with an exception e then the new, pending future is also completed with e
19
![Page 76: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/76.jpg)
Philipp Haller
FlatMap Combinator• Creates a new future by applying a function to the
successful result of the receiver future
• The future result of the function application determines the result of the new future
• If the function application results in an uncaught exception e then the new future is completed with e
• If the receiver future is completed with an exception e then the new, pending future is also completed with e
19
def flatMap[S](f: T => Future[S])(implicit ..): Future[S]
![Page 77: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/77.jpg)
Philipp Haller
Creating Futures
20
![Page 78: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/78.jpg)
Philipp Haller
Creating Futures• Futures are created based on (a) computations, (b) events,
or (c) combinations thereof
20
![Page 79: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/79.jpg)
Philipp Haller
Creating Futures• Futures are created based on (a) computations, (b) events,
or (c) combinations thereof
• Creating computation-based futures:
20
object Future {
def apply[T](body: => T)(implicit ..): Future[T]
}
![Page 80: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/80.jpg)
Philipp Haller
Creating Futures• Futures are created based on (a) computations, (b) events,
or (c) combinations thereof
• Creating computation-based futures:
20
object Future {
def apply[T](body: => T)(implicit ..): Future[T]
}Singleton object
![Page 81: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/81.jpg)
Philipp Haller
Creating Futures• Futures are created based on (a) computations, (b) events,
or (c) combinations thereof
• Creating computation-based futures:
20
object Future {
def apply[T](body: => T)(implicit ..): Future[T]
}Singleton object
“Code block” with result type T
![Page 82: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/82.jpg)
Philipp Haller
Creating Futures• Futures are created based on (a) computations, (b) events,
or (c) combinations thereof
• Creating computation-based futures:
20
object Future {
def apply[T](body: => T)(implicit ..): Future[T]
}Singleton object
“Code block” with result type T
“Unrelated” to the singleton
object!
![Page 83: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/83.jpg)
Philipp Haller
Futures: Example
21
![Page 84: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/84.jpg)
Philipp Haller
Futures: Example
21
val firstGoodDeal = Future { usedCars.find(car => isGoodDeal(car)) }
![Page 85: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/85.jpg)
Philipp Haller
Futures: Example
21
val firstGoodDeal = Future { usedCars.find(car => isGoodDeal(car)) }
val firstGoodDeal = Future.apply({ usedCars.find(car => isGoodDeal(car)) })
Short syntax for:
![Page 86: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/86.jpg)
Philipp Haller
Futures: Example
21
val firstGoodDeal = Future { usedCars.find(car => isGoodDeal(car)) }
val firstGoodDeal = Future.apply({ usedCars.find(car => isGoodDeal(car)) })
Short syntax for:
Type inference:val firstGoodDeal = Future.apply[Option[Car]]({ usedCars.find(car => isGoodDeal(car)) })
![Page 87: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/87.jpg)
Philipp Haller
Futures: Example
21
val firstGoodDeal = Future { usedCars.find(car => isGoodDeal(car)) }
val firstGoodDeal = Future.apply({ usedCars.find(car => isGoodDeal(car)) })
Short syntax for:
Type inference:val firstGoodDeal = Future.apply[Option[Car]]({ usedCars.find(car => isGoodDeal(car)) })
Type Future[Option[Car]]
![Page 88: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/88.jpg)
Philipp Haller
Creating Futures: Operationally
22
![Page 89: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/89.jpg)
Philipp Haller
Creating Futures: Operationally• Invoking the shown factory method creates a task object
encapsulating the computation
22
![Page 90: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/90.jpg)
Philipp Haller
Creating Futures: Operationally• Invoking the shown factory method creates a task object
encapsulating the computation
• The task object is scheduled for execution by an execution context
22
![Page 91: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/91.jpg)
Philipp Haller
Creating Futures: Operationally• Invoking the shown factory method creates a task object
encapsulating the computation
• The task object is scheduled for execution by an execution context
• An execution context is an object capable of executing tasks, typically using a thread pool
22
![Page 92: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/92.jpg)
Philipp Haller
Creating Futures: Operationally• Invoking the shown factory method creates a task object
encapsulating the computation
• The task object is scheduled for execution by an execution context
• An execution context is an object capable of executing tasks, typically using a thread pool
• Future tasks are submitted to the current implicit execution context
22
def apply[T](body: => T)(implicit executor: ExecutionContext): Future[T]
![Page 93: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/93.jpg)
Philipp Haller
Implicit Execution ContextsImplicit parameter requires selecting a execution context
23
![Page 94: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/94.jpg)
Philipp Haller
Implicit Execution ContextsImplicit parameter requires selecting a execution context
23
Welcome to Scala version 2.11.6 (Java HotSpot(TM) ..). Type in expressions to have them evaluated. Type :help for more information.
scala> import scala.concurrent._ import scala.concurrent._
scala> val fut = Future { 40 + 2 }
![Page 95: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/95.jpg)
Philipp Haller
Implicit Execution ContextsImplicit parameter requires selecting a execution context
23
<console>:10: error: Cannot find an implicit ExecutionContext. You might pass
Welcome to Scala version 2.11.6 (Java HotSpot(TM) ..). Type in expressions to have them evaluated. Type :help for more information.
scala> import scala.concurrent._ import scala.concurrent._
scala> val fut = Future { 40 + 2 }
![Page 96: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/96.jpg)
Philipp Haller
Implicit Execution ContextsImplicit parameter requires selecting a execution context
23
???
<console>:10: error: Cannot find an implicit ExecutionContext. You might pass
Welcome to Scala version 2.11.6 (Java HotSpot(TM) ..). Type in expressions to have them evaluated. Type :help for more information.
scala> import scala.concurrent._ import scala.concurrent._
scala> val fut = Future { 40 + 2 }
![Page 97: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/97.jpg)
Philipp Haller
Implicit Execution ContextsImplicit parameter requires selecting a execution context
23
<console>:10: error: Cannot find an implicit ExecutionContext. You might pass
Welcome to Scala version 2.11.6 (Java HotSpot(TM) ..). Type in expressions to have them evaluated. Type :help for more information.
scala> import scala.concurrent._ import scala.concurrent._
scala> val fut = Future { 40 + 2 } But…
![Page 98: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/98.jpg)
Philipp Haller
Implicit Execution ContextsImplicit parameter requires selecting a execution context
23
an (implicit ec: ExecutionContext) parameter to your method or import scala.concurrent.ExecutionContext.Implicits.global. val fut = Future { 40 + 2 } ^
<console>:10: error: Cannot find an implicit ExecutionContext. You might pass
Welcome to Scala version 2.11.6 (Java HotSpot(TM) ..). Type in expressions to have them evaluated. Type :help for more information.
scala> import scala.concurrent._ import scala.concurrent._
scala> val fut = Future { 40 + 2 }
![Page 99: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/99.jpg)
Philipp Haller
Promise
24
![Page 100: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/100.jpg)
Philipp Haller
Promise
Main purpose: create futures for non-lexically-scoped asynchronous code
24
![Page 101: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/101.jpg)
Philipp Haller
Promise
Main purpose: create futures for non-lexically-scoped asynchronous code
24
Example
Function for creating a Future that is completed with value after delay milliseconds
![Page 102: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/102.jpg)
Philipp Haller
Promise
Main purpose: create futures for non-lexically-scoped asynchronous code
24
def after[T](delay: Long, value: T): Future[T]
Example
Function for creating a Future that is completed with value after delay milliseconds
![Page 103: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/103.jpg)
Philipp Haller
“after”, Version 1
25
def after1[T](delay: Long, value: T) = Future { Thread.sleep(delay) value }
![Page 104: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/104.jpg)
Philipp Haller
“after”, Version 1
26
How does it behave?
![Page 105: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/105.jpg)
Philipp Haller
“after”, Version 1
26
assert(Runtime.getRuntime() .availableProcessors() == 8)
for (_ <- 1 to 8) yield after1(1000, true)
val later = after1(1000, true)
How does it behave?
![Page 106: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/106.jpg)
Philipp Haller
“after”, Version 1
26
assert(Runtime.getRuntime() .availableProcessors() == 8)
for (_ <- 1 to 8) yield after1(1000, true)
val later = after1(1000, true)
How does it behave?
Quiz: when is “later” completed?
![Page 107: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/107.jpg)
Philipp Haller
“after”, Version 1
26
assert(Runtime.getRuntime() .availableProcessors() == 8)
for (_ <- 1 to 8) yield after1(1000, true)
val later = after1(1000, true)
How does it behave?
Quiz: when is “later” completed?
Answer: after either ~1 s or ~2 s (most often)
![Page 108: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/108.jpg)
Philipp Haller
Promise
27
object Promise { def apply[T](): Promise[T] }
![Page 109: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/109.jpg)
Philipp Haller
Promise
27
object Promise { def apply[T](): Promise[T] }
trait Promise[T] { def success(value: T): Promise[T] def failure(cause: Throwable): Promise[T]
def future: Future[T] }
![Page 110: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/110.jpg)
Philipp Haller
“after”, Version 2
28
def after2[T](delay: Long, value: T) = { val promise = Promise[T]() timer.schedule(new TimerTask { def run(): Unit = promise.success(value) }, delay) promise.future }
![Page 111: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/111.jpg)
Philipp Haller
“after”, Version 2
28
def after2[T](delay: Long, value: T) = { val promise = Promise[T]() timer.schedule(new TimerTask { def run(): Unit = promise.success(value) }, delay) promise.future }
Much better behaved!
![Page 112: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/112.jpg)
Philipp Haller
What is Async?
• Scala module
• "org.scala-lang.modules" %% "scala-async"
• Purpose: simplify programming with futures
• Scala Improvement Proposal SIP-22
• Releases for Scala 2.10 and 2.11
29
![Page 113: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/113.jpg)
Philipp Haller
What Async Provides
• Future and Promise provide types and operations for managing data flow
• Very little support for control flow
• Async complements Future and Promise with constructs to manage control flow
30
![Page 114: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/114.jpg)
Philipp Haller
Programming Model
Basis: suspendible computations
31
![Page 115: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/115.jpg)
Philipp Haller
Programming Model
Basis: suspendible computations
• async { .. } — delimit suspendible computation
31
![Page 116: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/116.jpg)
Philipp Haller
Programming Model
Basis: suspendible computations
• async { .. } — delimit suspendible computation
• await(future) — suspend computation until
future is completed
31
![Page 117: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/117.jpg)
Philipp Haller
Async
32
![Page 118: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/118.jpg)
Philipp Haller
Async
32
object Async {
def async[T](body: => T): Future[T]
def await[T](future: Future[T]): T
}
![Page 119: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/119.jpg)
Philipp Haller
Example
33
val fstGoodDeal: Future[Option[Car]] = .. val sndGoodDeal: Future[Option[Car]] = ..
val goodCar = async { val car1 = await(fstGoodDeal).get val car2 = await(sndGoodDeal).get if (car1.price < car2.price) car1 else car2 }
![Page 120: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/120.jpg)
Philipp Haller
Futures vs. Async
• “Futures and Async: When to Use Which?”, Scala Days 2014, Berlin
• Video:
34
https://www.parleys.com/tutorial/futures-async-when-use-which
![Page 121: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/121.jpg)
Philipp Haller
Async in Other Languages
Constructs similar to async/await are found in a number of widely-used languages:
• C#
• Dart (Google)
• Hack (Facebook)
• ECMAScript 7 1
35
1 http://tc39.github.io/ecmascript-asyncawait/
![Page 122: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/122.jpg)
Philipp Haller
From Futures to Actors
• Limitations of futures:
• At most one completion event per future
• Overhead when creating many futures
• How to model distributed systems?
36
![Page 123: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/123.jpg)
Philipp Haller
The Actor Model
37
![Page 124: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/124.jpg)
Philipp Haller
The Actor Model• Model of concurrent computation whose universal primitive is
the “actor” [Hewitt et al. ’73]
37
![Page 125: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/125.jpg)
Philipp Haller
The Actor Model• Model of concurrent computation whose universal primitive is
the “actor” [Hewitt et al. ’73]
• Actors = concurrent “processes” communicating via asynchronous messages
37
![Page 126: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/126.jpg)
Philipp Haller
The Actor Model• Model of concurrent computation whose universal primitive is
the “actor” [Hewitt et al. ’73]
• Actors = concurrent “processes” communicating via asynchronous messages
37
Related to active objects
![Page 127: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/127.jpg)
Philipp Haller
The Actor Model• Model of concurrent computation whose universal primitive is
the “actor” [Hewitt et al. ’73]
• Actors = concurrent “processes” communicating via asynchronous messages
• Upon reception of a message, an actor may
• change its behavior/state
• send messages to actors (including itself)
• create new actors
37
Related to active objects
![Page 128: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/128.jpg)
Philipp Haller
The Actor Model• Model of concurrent computation whose universal primitive is
the “actor” [Hewitt et al. ’73]
• Actors = concurrent “processes” communicating via asynchronous messages
• Upon reception of a message, an actor may
• change its behavior/state
• send messages to actors (including itself)
• create new actors
• Fair scheduling
37
Related to active objects
![Page 129: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/129.jpg)
Philipp Haller
The Actor Model• Model of concurrent computation whose universal primitive is
the “actor” [Hewitt et al. ’73]
• Actors = concurrent “processes” communicating via asynchronous messages
• Upon reception of a message, an actor may
• change its behavior/state
• send messages to actors (including itself)
• create new actors
• Fair scheduling
• Decoupling: message sender cannot fail due to receiver
37
Related to active objects
![Page 130: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/130.jpg)
Philipp Haller
Example
38
class ActorWithTasks(tasks: ...) extends Actor { ...
def receive = { case TaskFor(workers) => val from = sender
val requests = (tasks zip workers).map { case (task, worker) => worker ? task } val allDone = Future.sequence(requests) allDone andThen { seq => from ! seq.mkString(",") } } }
![Page 131: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/131.jpg)
Philipp Haller
Example
38
class ActorWithTasks(tasks: ...) extends Actor { ...
def receive = { case TaskFor(workers) => val from = sender
val requests = (tasks zip workers).map { case (task, worker) => worker ? task } val allDone = Future.sequence(requests) allDone andThen { seq => from ! seq.mkString(",") } } } Using Akka (http://akka.io/)
![Page 132: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/132.jpg)
Philipp Haller
Anatomy of an Actor (1)• An actor is an active object with its own behavior
• Actor behavior defined by:
• subclassing Actor
• implementing def receive
39
class ActorWithTasks(tasks: List[Task]) extends Actor { def receive = { case TaskFor(workers) => // send `tasks` to `workers` case Stop => // stop `self` } }
![Page 133: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/133.jpg)
Philipp Haller
Anatomy of an Actor (2)• Exchanged messages should be immutable
• And serializable, to enable remote messaging
• Message types should implement structural equality
• In Scala: case classes and case objects
• Enables pattern matching on the receiver side
40
case class TaskFor(workers: List[ActorRef]) case object Stop
![Page 134: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/134.jpg)
Philipp Haller
Anatomy of an Actor (3)• Actors are isolated
• Strong encapsulation of state
• Requires restricting access and creation
• Separate Actor instance and ActorRef
• ActorRef public, safe interface to actor
41
val system = ActorSystem(“test-system”) val actor1: ActorRef = system.actorOf[ActorWithTasks]
actor1 ! TaskFor(List()) // async message send
![Page 135: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/135.jpg)
Philipp Haller
Why Actors?
42
![Page 136: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/136.jpg)
Philipp Haller
Why Actors?Reason 1: simplified concurrency
42
![Page 137: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/137.jpg)
Philipp Haller
Why Actors?Reason 1: simplified concurrency
• “Share nothing”: strong isolation of actors ➟ no race conditions
42
![Page 138: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/138.jpg)
Philipp Haller
Why Actors?Reason 1: simplified concurrency
• “Share nothing”: strong isolation of actors ➟ no race conditions
• Actors handle at most one message at a time ➟ sequential reasoning
42
![Page 139: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/139.jpg)
Philipp Haller
Why Actors?Reason 1: simplified concurrency
• “Share nothing”: strong isolation of actors ➟ no race conditions
• Actors handle at most one message at a time ➟ sequential reasoning
42
“Macro-step semantics”
![Page 140: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/140.jpg)
Philipp Haller
Why Actors?Reason 1: simplified concurrency
• “Share nothing”: strong isolation of actors ➟ no race conditions
• Actors handle at most one message at a time ➟ sequential reasoning
• Asynchronous message handling ➟ less risk of deadlocks
42
“Macro-step semantics”
![Page 141: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/141.jpg)
Philipp Haller
Why Actors?Reason 1: simplified concurrency
• “Share nothing”: strong isolation of actors ➟ no race conditions
• Actors handle at most one message at a time ➟ sequential reasoning
• Asynchronous message handling ➟ less risk of deadlocks
• No “inversion of control”: access to own state and messages in safe, direct way
42
“Macro-step semantics”
![Page 142: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/142.jpg)
Philipp Haller
Why Actors? (cont’d)
43
![Page 143: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/143.jpg)
Philipp Haller
Why Actors? (cont’d)Reason 2: actors model reality of distributed systems
43
![Page 144: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/144.jpg)
Philipp Haller
Why Actors? (cont’d)Reason 2: actors model reality of distributed systems
• Message sends truly asynchronous
43
![Page 145: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/145.jpg)
Philipp Haller
Why Actors? (cont’d)Reason 2: actors model reality of distributed systems
• Message sends truly asynchronous
• Message reception not guaranteed
43
![Page 146: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/146.jpg)
Philipp Haller
Why Actors? (cont’d)Reason 2: actors model reality of distributed systems
• Message sends truly asynchronous
• Message reception not guaranteed
• Non-deterministic message ordering
43
![Page 147: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/147.jpg)
Philipp Haller
Why Actors? (cont’d)Reason 2: actors model reality of distributed systems
• Message sends truly asynchronous
• Message reception not guaranteed
• Non-deterministic message ordering
• Some implementations preserve message ordering between pairs of actors
43
![Page 148: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/148.jpg)
Philipp Haller
Why Actors? (cont’d)Reason 2: actors model reality of distributed systems
• Message sends truly asynchronous
• Message reception not guaranteed
• Non-deterministic message ordering
• Some implementations preserve message ordering between pairs of actors
Therefore, actors well-suited as a foundation for distributed systems
43
![Page 149: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/149.jpg)
Philipp Haller
Summary
44
![Page 150: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/150.jpg)
Philipp Haller
Summary
• Concurrency benefits from growable languages
44
![Page 151: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/151.jpg)
Philipp Haller
Summary
• Concurrency benefits from growable languages
• Futures and promises a versatile abstraction for single, asynchronous events
44
![Page 152: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/152.jpg)
Philipp Haller
Summary
• Concurrency benefits from growable languages
• Futures and promises a versatile abstraction for single, asynchronous events
• Supported by async/await
44
![Page 153: Futures, Actors, and Streamsstg-tud.github.io/ctbd/2016/CTBD_09_futures_actors.pdf · Futures, Actors, and Streams. Philipp Haller Programming a Concurrent World ... • Contributed](https://reader034.vdocuments.mx/reader034/viewer/2022042223/5ec97b1f87925279d72b98a8/html5/thumbnails/153.jpg)
Philipp Haller
Summary
• Concurrency benefits from growable languages
• Futures and promises a versatile abstraction for single, asynchronous events
• Supported by async/await
• The actor model faithfully models distributed systems
44