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
780
Shapeless Introduction
pishen
2
860
ScalaKitchen
pishen
1
370
sbt-emr-spark
pishen
1
100
My Personal Report of Scala Kansai 2016
pishen
0
230
SBT Basic Concepts
pishen
1
560
Featured
See All Featured
Navigating Team Friction
lara
183
14k
Being A Developer After 40
akosma
87
590k
Code Review Best Practice
trishagee
64
17k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
The Cult of Friendly URLs
andyhume
78
6k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.3k
How To Stay Up To Date on Web Technology
chriscoyier
788
250k
Art, The Web, and Tiny UX
lynnandtonic
297
20k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.1k
Done Done
chrislema
181
16k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
6.9k
Gamification - CAS2011
davidbonilla
80
5k
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~