Practical introduction to the actor model

1,414 views
1,240 views

Published on

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,414
On SlideShare
0
From Embeds
0
Number of Embeds
31
Actions
Shares
0
Downloads
35
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Practical introduction to the actor model

    1. 1. Practical intro to the Actor Model Georgios Gousios TU Delft
    2. 2. Challenges with Concurrency• Amdhal’s law• Shared state• Locking, concurrency protocols• Only way to increase execution speed http://en.wikipedia.org/w/index.php?title=File:AmdahlsLaw.svg
    3. 3. Actors• Unit of computation, encapsulates • Processing • Storage • Communication• 3 axioms. An actor can: • Create new actors • Send messages to actors it knows about • Handle incoming messages
    4. 4. Actors in practice• Actors maintain internal state • Promise: Only one thread can process msgs for a single actor at any time• Actors have mailboxes• Messages must be immutable• Actors are supervised by other actors
    5. 5. Supervision hierarchies Sup A AC D
    6. 6. Actors in practice• Work in a container • Actor registry • Manages assignment of actors to processors • Fault tolerance, supervision• Can be distributed (explicitly/implicitly)• Can be hot swapped
    7. 7. A simple Actorcase class Msg(contents: String)class ActorExample extends Actor { def receive = { case Msg(contents) => print(contents) case _ => "Dont know what to do" }}
    8. 8. An Actor with statecase object Addcase object Addedclass ActorExample extends Actor { var state = 0 def receive = { case Add => state += 1 sender ! Added case _ => "Dont know what to do with it" }}
    9. 9. Types of communication• Fire and forget• Fire and get Future• Fire and wait • BAD • BAD • BAD
    10. 10. Futures• Represent a concurrent computation• In functional languages, they are... Monads! val f1 = Future { "Hello" + "World" } val f2 = f1 map { x x.length }
    11. 11. Futures as Monadsval f1 = ask(actor1, msg1)val f2 = ask(actor2, msg2)val f3 = for { a ← f1.mapTo[Int] b ← f2.mapTo[Int] c ← ask(actor3, (a + b)).mapTo[Int]} yield cval r = Await.result(f3,1).asInstanceOf[Int]
    12. 12. Failure management• Let it crash• Supervisor knows an exception is thrown • Respawns crashed actor• Simple as that! • caveat: actor mailbox?
    13. 13. Language support• Erlang, it all started here• Scala /Akka•D• Dart• Libraries for most other languages
    14. 14. http://bit.ly/hewitt-on-actors

    ×