Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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
Tweet

More Decks by Rahul Goma Phulore

Other Decks in Technology

Transcript

  1. What  we  are  going  to  talk  about   •  How

     concurrency  and  parallelism  are  different   ideas   •  Why  concurrency  is  important   •  Futures  and  promises     •  DEMO!   •  PiGalls  
  2. 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. Orthogonality  illustrated   Javascript   promises,  Python   generators  

    Java  and  Scala   futures   Well…  lots  of   things   Parallel  collecJons  
  4. Age  of  mulJcore   •  MulJcore  has  become  a  norm!

      •  Need  to  exploit  parallelism  to  perform   •  Good  concurrency  abstracJons  a  way  to   get  there  
  5. Futures  and  promises   •  Can  be  thought  of  as

     a  single  concurrency   abstracJon  
  6. 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. 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. There  is  a  soluJon!   •  Higher-­‐order  funcJons  /  combinators

      •  for-­‐comprehensions   •  Other  abstracJons  
  9. 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. 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. 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. 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