Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Async Programming & Actor Model
Search
Pishen Tsai
July 24, 2015
0
68
Async Programming & Actor Model
Pishen Tsai
July 24, 2015
Tweet
Share
More Decks by Pishen Tsai
See All by Pishen Tsai
Introduction to Minitime
pishen
1
120
都什麼時代了,你還在寫 while loop 嗎?
pishen
2
700
Pishen's Emacs Journey
pishen
0
120
Scala + Google Dataflow = Serverless Spark
pishen
6
790
Shapeless Introduction
pishen
2
870
ScalaKitchen
pishen
1
380
sbt-emr-spark
pishen
1
110
My Personal Report of Scala Kansai 2016
pishen
0
230
SBT Basic Concepts
pishen
1
570
Featured
See All Featured
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
44
9.3k
Site-Speed That Sticks
csswizardry
2
190
Speed Design
sergeychernyshev
25
670
GitHub's CSS Performance
jonrohan
1030
460k
Agile that works and the tools we love
rasmusluckow
328
21k
Building Better People: How to give real-time feedback that sticks.
wjessup
365
19k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
132
33k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
Writing Fast Ruby
sferik
628
61k
Side Projects
sachag
452
42k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.4k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
Transcript
Async Programming & Actor Model @pishen
Asynchronous programming val a: Future[Int] = Future { //time-consuming process
result } a.map(x => x + 1) callback listener
Asynchronous programming val a: Future[Int] = ??? def f(x: Int):
Future[Int] = ??? val r = a.map(x => f(x)) Future[Future[Int]]
Asynchronous programming val a: Future[Int] = ??? def f(x: Int):
Future[Int] = ??? val r = a.flatMap(x => f(x)) Future[Int]
Asynchronous programming val a: Future[Int] = ??? def f(x: Int):
Future[Int] = ??? val r = a.flatMap(x => f(x)) val p = Await.result(r, 3.seconds) Int
T T T T T T T Future { ???
} a.map(???) ??? Await(???)
T T T T T T T Future { ???
} a.map(???) F ??? Await(???)
T T T T T T T Future { ???
} a.map(???) F ??? Await(???)
T T T T T T T Future { ???
} a.map(???) F ??? F Await(???)
T T T T T T T Future { ???
} a.map(???) F ??? F Await(???)
T T T T T T Future { ??? }
a.map(???) F F ??? Await(???) T
T T T T T T Future { ??? }
a.map(???) F F ??? Await(???) T
T T T T T T Future { ??? }
a.map(???) F F ??? Await(???) T
T T T T T T Future { ??? }
a.map(???) F F ??? Await(???) T
T T T T T T Future { ??? }
a.map(???) F F ??? Await(???) T
T T T T T T Future { ??? }
a.map(???) F F ??? Await(???) T C C
T R
T R R R
T R R R C C C C C C
C C
T R C C C C C C C C
T T R R
T R C C C C C C C C
T T R R R R R R R R R R R R R R 1000+
Orz T T R C C C C C C
C T T R R R R R R R R R R R R R R 1000+ T T T T T T T T T switching thread takes time... crashed, nothing get computed after all...
R C C C C C C C T R
R R R R R R R R R R R R R 1000+ T thread pool C T T T T T T
R C C C C C C C T R
R R R R R R R R R R R R R T C T T T T T T access DB long computing
R C C C C C C C T R
R R R R R R R R R R R R R T C T T T T T T access DB long computing blocking
R C C C C C C C T R
R R R R R R R R R R R R R C T T T T T T T T T T T T T T T T T T T T T T T T T T T T T F F F F F F 1000+ async squeeze out the performance of your machine
Life is beautiful ...before you have states.
HP MP position weapon exp hunger Running Attacked Attacking Eating
HP MP position weapon exp hunger Running Attacked Attacking Need
lock(s) to prevent strange things happen. Eating
HP MP position weapon exp hunger Running Attacked Attacking No
matter you forward the job to DB or what. Eating
HP MP position weapon exp hunger Running Attacked Attacking All
the other threads get blocked. Eating
Can we get rid of the locks?
P W Wrap the states in an Actor. T m
h p a e mailbox
P W Wrap the states in an Actor. T m
h p a e mailbox T
P W Wrap the states in an Actor. T m
h p a e mailbox T
T T T T T T T T T T
T T T T T T T front m back
T T T T T T T T T T
T T T T T T T front m l back
T T T T T T T T T T
T T T T T T T front m l back Disk
Life is beautiful ...before you have many machines.
T T T T T T T T T T
T T T T T T T front m l back Machine A T T T T T T s Machine B Disk
Tell & Ask val a: ActorRef = ctx.actorOf(???) a !
“Hello!” (a ? “What’s your name?”) .mapTo[String] .map(name => ???)
Receive messages class MyActor extends Actor { def receive =
{ case “Hello!” => ??? case “What’s your name?” => sender ! “Pishen” } }
Create the Future[T] with Scala~