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
130
都什麼時代了,你還在寫 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
250
SBT Basic Concepts
pishen
1
570
Featured
See All Featured
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
3
360
No one is an island. Learnings from fostering a developers community.
thoeni
19
3.1k
Adopting Sorbet at Scale
ufuk
74
9.2k
How STYLIGHT went responsive
nonsquared
96
5.3k
For a Future-Friendly Web
brad_frost
176
9.5k
Scaling GitHub
holman
459
140k
Docker and Python
trallard
43
3.2k
Optimising Largest Contentful Paint
csswizardry
33
3k
Building Adaptive Systems
keathley
38
2.4k
4 Signs Your Business is Dying
shpigford
182
22k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
The Pragmatic Product Professional
lauravandoore
32
6.4k
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~