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
A better future with KSS
kneath
238
17k
Reflections from 52 weeks, 52 projects
jeffersonlam
346
20k
Intergalactic Javascript Robots from Outer Space
tanoku
269
27k
BBQ
matthewcrist
85
9.3k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
44
2.2k
Gamification - CAS2011
davidbonilla
80
5k
Six Lessons from altMBA
skipperchong
27
3.5k
Agile that works and the tools we love
rasmusluckow
327
21k
Art, The Web, and Tiny UX
lynnandtonic
297
20k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
Facilitating Awesome Meetings
lara
50
6.1k
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~