Slide 1

Slide 1 text

Promise  of  a  be,er   future   Pooja  Akshantal   Rahul  Goma  Phulore  

Slide 2

Slide 2 text

Who  are  we?  

Slide 3

Slide 3 text

What  we  are  going  to  talk  about   •  How  concurrency  and  parallelism  are  different   ideas   •  Why  concurrency  is  important   •  Futures  and  promises     •  DEMO!   •  PiGalls  

Slide 4

Slide 4 text

Concurrency  ≠  Parallelism   Concurrency  is  a  program-­‐ structuring  technique  in  which   there  are  mulJple  threads  of   control   A  parallel  program  is  one  that   uses  a  mul5plicity  of   computa5onal  hardware  (e.g.   mulJple  processor  cores)   Structure   Execu5on   Dealing  with  lots  of  things  at   once   Doing  lots  of  things  at  once    

Slide 5

Slide 5 text

Orthogonality  illustrated   Javascript   promises,  Python   generators   Java  and  Scala   futures   Well…  lots  of   things   Parallel  collecJons  

Slide 6

Slide 6 text

Fall  of  Moore’s  law  

Slide 7

Slide 7 text

Age  of  mulJcore   •  MulJcore  has  become  a  norm!   •  Need  to  exploit  parallelism  to  perform   •  Good  concurrency  abstracJons  a  way  to   get  there  

Slide 8

Slide 8 text

Futures  and  promises  

Slide 9

Slide 9 text

Futures  and  promises   •  Can  be  thought  of  as  a  single  concurrency   abstracJon  

Slide 10

Slide 10 text

Future  ≈  Cheap  thread   •  Futures  are  mulJplexed  dynamically  onto   threads  as  needed.     •  Managed  by  Execu5onContext.   •  It’s  pracJcal  to  have  hundreds  of  thousands   going  at  once.   •  Spawn  away!    

Slide 11

Slide 11 text

Code  Time  

Slide 12

Slide 12 text

Async  and  non-­‐blocking   •  Goal:  Do  not  block  current  thread  while   waiJng  for  the  result  of  the  future.   •  Callbacks:   – Register  callback  which  is  invoked  asynchronously   when  future  is  completed   – Async  computa5ons  do  not  block  

Slide 13

Slide 13 text

Callbacks?!  

Slide 14

Slide 14 text

There  is  a  soluJon!   •  Higher-­‐order  funcJons  /  combinators   •  for-­‐comprehensions   •  Other  abstracJons  

Slide 15

Slide 15 text

Running  example   •  Collect  staJsJcs  for  a     facebook  page   •  For  every  post  on  the     page:   – Collect  all  likes   – Collect  all  comments   •  Aggregate  results  into  popularity  score  

Slide 16

Slide 16 text

Code  Time  

Slide 17

Slide 17 text

Other  parallels   •  Error  recovery  combinators   –  recover   –  recoverWith   •  CollecJon  operaJons   –  map: (Seq[A], A ⇒ B) ⇒ Seq[B]
 
 mapF: (Seq[A], A ⇒ Future[B]) ⇒ Future[Seq[B]]
 –  filter: (Seq[A], A ⇒ Boolean) ⇒ Seq[A]
 
 filterF: (Seq[A], A ⇒ Future[Boolean]) ⇒ Future[Seq[A]]

Slide 18

Slide 18 text

PiGalls   •  InteracJons  with  thread-­‐unsafe  libraries   •  ThreadLocal  variables   •  Retries,  Jme-­‐outs  not  very  natural   •  Simpler  than  other  concurrency  models  in   many  ways,  but  sJll  incur  some  cogniJve  cost  

Slide 19

Slide 19 text

Credits   •  Heather  Miller,  for  her  material  on   relaJonship  between  futures  and  promises   •  Simon  Marlow  and  Rob  Pike,  for  their   material  on  concurrency  and  parallelism   •  Josh  Suereth,  for  his  Github  example   (InspiraJon  for  our  facebook  example)   •  Scala,  Play,  and  Akka  teams  for  all  their   amazing  work  

Slide 20

Slide 20 text

Thank  You!   ☺