Promise of a better future

Promise of a better future

Slides for a talk Pooja Akshantal and I gave at Pune Scala Symposium, 2014.


Rahul Goma Phulore

April 12, 2014


  1. 3.

    What  we  are  going  to  talk  about   •  How

     concurrency  and  parallelism  are  different   ideas   •  Why  concurrency  is  important   •  Futures  and  promises     •  DEMO!   •  PiGalls  
  2. 4.

    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    
  3. 5.

    Orthogonality  illustrated   Javascript   promises,  Python   generators  

    Java  and  Scala   futures   Well…  lots  of   things   Parallel  collecJons  
  4. 7.

    Age  of  mulJcore   •  MulJcore  has  become  a  norm!

      •  Need  to  exploit  parallelism  to  perform   •  Good  concurrency  abstracJons  a  way  to   get  there  
  5. 9.

    Futures  and  promises   •  Can  be  thought  of  as

     a  single  concurrency   abstracJon  
  6. 10.

    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!    
  7. 12.

    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  
  8. 14.

    There  is  a  soluJon!   •  Higher-­‐order  funcJons  /  combinators

      •  for-­‐comprehensions   •  Other  abstracJons  
  9. 15.

    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  
  10. 17.

    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]]
  11. 18.

    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  
  12. 19.

    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