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
Writing Fast Ruby
sferik
628
61k
Why Our Code Smells
bkeepers
PRO
335
57k
Statistics for Hackers
jakevdp
796
220k
A Philosophy of Restraint
colly
203
16k
How To Stay Up To Date on Web Technology
chriscoyier
789
250k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
2
290
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
Reflections from 52 weeks, 52 projects
jeffersonlam
347
20k
We Have a Design System, Now What?
morganepeng
51
7.3k
How GitHub (no longer) Works
holman
311
140k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
Building Adaptive Systems
keathley
38
2.3k
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~