0
ASYNCHRONOUSPROGRAMMING    FTW!    (SAWYER X)
DOES THIS SOUND FAMILIAR?               "Hi"            "Hi, dad"         "Got a minute?"              "Yes"     "Im looki...
............
"Well?""Ah! It loaded! What now?"  "Click on new movies"         "OK..."           ........         "Well?"        "Loadin...
"OK, now what?"      "Find a movie you want"            "Then what?""Click on it, youll have instructions"        * eterni...
ILLUSTRATED          (USING WWW::MECHANIZE)$eh>e(cnm_r) mc­gt$ieaul;i  mc­sces){ f($eh>ucs    $eh>lc(NwMve;   mc­cike ois)...
LETS TRY THIS AGAIN...                 "Hi"               "Hi, dad"            "Got a minute?"                 "Yes"      ...
GREAT SUCCESS!
CALLBACKS        Reference to a code        Passed as an argument        Can be called by some other codesbtysmtig{ u r_oe...
ILLUSTRATED, ASYNC          (USING WWW::MECHANIZE)$eh>e($ieaul u  mc­gt cnm_r,sb{  $eh>lc(e ois,sb{   mc­cik NwMve u     $...
WHY IS THIS USEFUL?Allow others to decide if something is good or not
m jn  uo>e( y$uo=Jn­nw  hss >[w11112222]   ot = q<... ...>,  cek >{   hcs=     Fig=      Pn >{      o_ucs >sb{       nsces...
WHY IS THIS USEFUL?Allow others to decide if something is good or notAllow easy stepping
Algorithm::Diff::CallbackueAgrtm:if:alakq<ifary ifhse> s loih:Df:Clbc wdf_rasdf_ahs;df_ras ifary(  odfml_ebr,   @l_aiymmes...
WHY IS THIS USEFUL?Allow others to decide if something is good or notAllow easy steppingAllow control by other process (a ...
THE EVENT LOOPCreate loopRegister eventsRun themLet the user keep registering new events
m eet  ..; y%vns=(.)wie(){ hl 1   frahm eet(ky eet     oec y$vn  es%vns){    $vn­(;     eet>)  }   }
m eet;%vns=(.) y%vns eet  ..;wie(){ hl 1   frahm eet(ky eet     oec y$vn  es%vns){    $vn­(;     eet>)  }   }wie(){ hl 1  ...
EVENT TYPESTimers (one-time, recurring, sleep replacement)I/O (input output, including networking)
FUCK TALKLETS GO PROGRAMMING
IO::ASYNCLoop interface: IO::Async::LoopYou can connect to hosts using itYou can add listeners to it (handles, sockets, et...
ueI:Ayc:tem s O:sn:Sra;ueI:Ayc:op s O:sn:Lo; m lo  O:sn:Lo­nw y$op=I:Ayc:op>e;$op>onc( lo­cnet  hs   = sm.te.ot,   ot   >"...
o_tem= u  nsra >sb{  m  sra   _   y($tem)=@;  $tem>ofgr(   sra­cniue    o_ed= u      nra >sb{      m  sl,$ufe,$o   _      ...
ANOTHER EXAMPLE$pr MO:sn Me:Ayc:TP­   el­I:Ayc­Nt:sn:HT E  I:Ayc>d(m ht  e:Ayc:TP>e ;   O:sn­ad y$tp=Nt:sn:HT­nw)  syfrI:A...
POELoop interface: POE::KernelRuns "sessions" (POE::Session)Each session is a context of eventsEach session has an ID and ...
POEuePE #At­nldsPE:enladPE:eso. s O;  uoicue O:Kre n O:Ssin#..dfn alak us(etsie .  . eieclbcssb nx ld)..fr(.1){ o 1.0   PE...
sbhnlrsat{ u ade_tr  m  kre,$ep ssin)=@[KRE,HA,SSIN]  y($enl ha,$eso   _ ENL EP ESO ; sy"eso ,$eso­I,"hssatd;  a Ssin" ssi...
sbhnlrsat{ u ade_tr  m  kre,$ep ssin)=@[KRE,HA,SSIN]  y($enl ha,$eso   _ ENL EP ESO ; sy"eso ,$eso­I,"hssatd;  a Ssin" ssi...
MORE CONDENSEDuePE s O;fr(.1){ o 1.0   m cut=0   y$on  ;  PE:eso­cet(   O:Ssin>rae    iln_tts=      niesae >{      _tr  = ...
REFLEXWritten by author of POE"How POE would be written if I had Moose back then"Stresses composability, reusabilityInclud...
USING MOOSE{  pcaeAp   akg p;  ueMoe   s os;  etnselx:ae;   xed Rfe:Bs  ueRfe:Itra;   s elx:nevl  ueRfe:Tat:ace wths;   s ...
COMPLEX? TRY THIS...ueRfe:Itra; s elx:nevl m t=Rfe:Itra­nw y$  elx:nevl>e(  itra  = ,   nevl  >1  at_eet= ,   uorpa >1  o_...
ANYEVENTAnyEvent is very thin and very fastWas written to support as many loops as possible... that conform to the authors...
ueAyvn; s nEetm cut=0 y$on  ;m c  =Ayvn­cnvr y$v   nEet>oda;m t  =Ayvn­tmr y$    nEet>ie(  atr  >2   fe  = ,  itra >06   n...
m w w=Ayvn­i( y$;$  nEet>o  f  >SDN   h = *TI,  pl >   ol= r,  c  >sb{   b = u     cop(m ipt= ;     hm  y$nu  )    sy"ed i...
USAGES OF ASYNCHRONOUS CODE     GUI     Services (HTTP/SMTP/etc.)     Performance     Off-loading tasks
THANK YOU
Upcoming SlideShare
Loading in...5
×

Asynchronous programming FTW!

685

Published on

These are the slides for the talk I gave at YAPC::EU 2012 called Asynchronous Programming FTW! It tries to explain what async programming is with examples of a few Perl async frameworks.

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
685
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
7
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Transcript of "Asynchronous programming FTW!"

  1. 1. ASYNCHRONOUSPROGRAMMING FTW! (SAWYER X)
  2. 2. DOES THIS SOUND FAMILIAR? "Hi" "Hi, dad" "Got a minute?" "Yes" "Im looking for a movie Where do I start?" "Go to the cinema website" "Whats the address?" "www.whatchamacallit.whatever" "Just a sec..."
  3. 3. ............
  4. 4. "Well?""Ah! It loaded! What now?" "Click on new movies" "OK..." ........ "Well?" "Loading" *sounds of clicks*
  5. 5. "OK, now what?" "Find a movie you want" "Then what?""Click on it, youll have instructions" * eternity passes by * "WELL?!?" "Just a sec... its loading..." /me shoots myself
  6. 6. ILLUSTRATED (USING WWW::MECHANIZE)$eh>e(cnm_r) mc­gt$ieaul;i  mc­sces){ f($eh>ucs    $eh>lc(NwMve;   mc­cike ois)  i  mc­sces){   f($eh>ucs      m mve  mc­fn_nus..     y@ois=$eh>idipt(.)  }   }
  7. 7. LETS TRY THIS AGAIN... "Hi" "Hi, dad" "Got a minute?" "Yes" "Im looking for a movie What should I do?""Go to this website, click on new movies Then pick a movie, and click on it" "Thank you" "Youre welcome"
  8. 8. GREAT SUCCESS!
  9. 9. CALLBACKS Reference to a code Passed as an argument Can be called by some other codesbtysmtig{ u r_oehn   m ipt  hf;   y$nu =sit  m c    hf;   y$b  =sit  m rsl  oeoeain$nu)   y$eut=sm_prto(ipt;  i  rsl­{sau qK     f($eut>tts}e O){    $b> rsl­{cnet})     c­($eut>otn ;  }   }tysmtig sm admipt,sb{ r_oehn("oerno nu" u   m cnet=sit   y$otn  hf;  sy"ucsflrqetsy cnet"   a Scesu eus as$otn!;})  ;
  10. 10. ILLUSTRATED, ASYNC (USING WWW::MECHANIZE)$eh>e($ieaul u  mc­gt cnm_r,sb{  $eh>lc(e ois,sb{   mc­cik NwMve u     $eh>idipt(.. u  y@ois=@       mc­fn_nus .,sb{m mve  _})  })    ;})  ;
  11. 11. WHY IS THIS USEFUL?Allow others to decide if something is good or not
  12. 12. m jn  uo>e( y$uo=Jn­nw  hss >[w11112222]   ot = q<... ...>,  cek >{   hcs=     Fig=      Pn >{      o_ucs >sb{       nsces= u         m  jn,$ot dt   _         y($uo hs,$aa)=@;        m  i,$os ag)=$uo>nlz_igrsl(         y($p ls,$v   jn­aayepn_eut          $aa>ter}           dt­{sdr,        )         ;        $ef>e_ot hs,$v ;         sl­sths($ot ag)      }       ,      o_al= u        nfi >sb{        m  jn,$ot)=@;         y($uo hs   _        $ef>eeehs(hs)         sl­dlt_ot$ot;      }       ,    }       }   );
  13. 13. WHY IS THIS USEFUL?Allow others to decide if something is good or notAllow easy stepping
  14. 14. Algorithm::Diff::CallbackueAgrtm:if:alakq<ifary ifhse> s loih:Df:Clbc wdf_rasdf_ahs;df_ras ifary(  odfml_ebr,   @l_aiymmes  nwfml_ebr,   @e_aiymmes  sb{pitap oha bu ,sit}   u  rn Hpyt eraot hf ,  sb{pitor oha bu ,sit}   u  rn Sryt eraot hf ,);df_ahs ifhse(  oddtis   %l_eal,  nwdtis   %e_eal,  sb{syot  hf ,   u  a Ls , sit}  sb{syand hf ,   u  a Gie ,sit}  sb{   u     m  ky bfr,$fe   _     y($e,$eoe atr)=@;    sy"kycagdfo bfr o$fe"     a $e hne rm$eoet atr;  }   ,);
  15. 15. WHY IS THIS USEFUL?Allow others to decide if something is good or notAllow easy steppingAllow control by other process (a type of IOC)Allow asynchronous code
  16. 16. THE EVENT LOOPCreate loopRegister eventsRun themLet the user keep registering new events
  17. 17. m eet  ..; y%vns=(.)wie(){ hl 1   frahm eet(ky eet     oec y$vn  es%vns){    $vn­(;     eet>)  }   }
  18. 18. m eet;%vns=(.) y%vns eet  ..;wie(){ hl 1   frahm eet(ky eet     oec y$vn  es%vns){    $vn­(;     eet>)  }   }wie(){ hl 1   frahm eet(ky eet     oec y$vn  es%vns){    i  eet{eet{tm =tm       f($vns$vn}ie}< ie){      $vns$vn}oe}>)       eet{eet{cd­(;    }       }   }sbadeet{ u d_vn   m  tm,$oe)=@;   y($ie cd   _  $vns$d{tm  tm;   eet{i}ie}=$ie  $vns$d{cd  cd;   eet{i}oe}=$oe  $d+   i+;}
  19. 19. EVENT TYPESTimers (one-time, recurring, sleep replacement)I/O (input output, including networking)
  20. 20. FUCK TALKLETS GO PROGRAMMING
  21. 21. IO::ASYNCLoop interface: IO::Async::LoopYou can connect to hosts using itYou can add listeners to it (handles, sockets, etc.)Explicitly call "run" when ready
  22. 22. ueI:Ayc:tem s O:sn:Sra;ueI:Ayc:op s O:sn:Lo; m lo  O:sn:Lo­nw y$op=I:Ayc:op>e;$op>onc( lo­cnet  hs   = sm.te.ot,   ot   >"oeohrhs"  srie = 24,   evc  >135  sctp = sra   okye >tem,    o_tem= u  . ,   nsra >sb{..}    o_eov_ro >sb{   nrsleerr= u     de"antrsle­$[1"     i Cno eov  _­]  }   ,  o_onc_ro >sb{   ncneterr= u     de"antcnet­$[]fie _­]     i Cno onc  _0 ald$[1"  }   ,);$op>u; lo­rn
  23. 23. o_tem= u  nsra >sb{  m  sra   _   y($tem)=@;  $tem>ofgr(   sra­cniue    o_ed= u      nra >sb{      m  sl,$ufe,$o   _       y($ef bfrf ef)=@;      wie {bfrf ~s^.)/){       hl($$ufe}= /(*n/          sy"eevdaln 1;         a Rcie  ie$"      }             rtr ;       eun0    }       )   ;  $tem>rt(A nta iehr;   sra­wieniiilln ee)  $op>d(sra)   lo­ad$tem;}
  24. 24. ANOTHER EXAMPLE$pr MO:sn Me:Ayc:TP­   el­I:Ayc­Nt:sn:HT E  I:Ayc>d(m ht  e:Ayc:TP>e ;   O:sn­ad y$tp=Nt:sn:HT­nw)  syfrI:Ayc>wi_l(mp{$tp>e(_  )   a o O:sn­aatal a  ht­gt$)} >ls­fUL.x   ito­Rstt Oh hai parallel HTTP GET!
  25. 25. POELoop interface: POE::KernelRuns "sessions" (POE::Session)Each session is a context of eventsEach session has an ID and heapCall events in a current or different sessionExplicitly call POE::Kernel->run when ready
  26. 26. POEuePE #At­nldsPE:enladPE:eso. s O;  uoicue O:Kre n O:Ssin#..dfn alak us(etsie .  . eieclbcssb nx ld)..fr(.1){ o 1.0   PE:eso­cet(   O:Ssin>rae    iln_tts=      niesae >{      _tr  = &ade_tr,       sat  >hnlrsat      iceet= &ade_nrmn,       nrmn >hnlriceet      _tp  = &ade_tp       so   >hnlrso,    }       )   ;}PE:enl>u(; O:Kre­rn)
  27. 27. sbhnlrsat{ u ade_tr  m  kre,$ep ssin)=@[KRE,HA,SSIN]  y($enl ha,$eso   _ ENL EP ESO ; sy"eso ,$eso­I,"hssatd;  a Ssin" ssin>D  a tre" $ep>on  ;  ha­{cut}=0 $enl>il(iceet)  kre­yednrmn;}sbhnlriceet{ u ade_nrmn  m  kre,$ep ssin)=@[KRE,HA,SSIN]  y($enl ha,$eso   _ ENL EP ESO ; sy"eso ,$eso­I,"cutdt ,+$ep>on;  a Ssin" ssin>D  one o" +ha­{cut} $enl>il(iceet)i ha­{cut}<1;  kre­yednrmn f$ep>on  0}sbhnlrso  u ade_tp{ sy"eso ,$[ESO]>D  a tpe"  a Ssin" _SSIN­I,"hssopd;}
  28. 28. sbhnlrsat{ u ade_tr  m  kre,$ep ssin)=@[KRE,HA,SSIN]  y($enl ha,$eso   _ ENL EP ESO ; sy"eso ,$eso­I,"hssatd;  a Ssin" ssin>D  a tre" $ep>on  ;  ha­{cut}=0 $enl>il(iceet)  kre­yednrmn;}sbhnlriceet{ u ade_nrmn  m  kre,$ep ssin)=@[KRE,HA,SSIN]  y($enl ha,$eso   _ ENL EP ESO ; sy"eso ,$eso­I,"cutdt ,+$ep>on;  a Ssin" ssin>D  one o" +ha­{cut} $enl>il(iceet)i ha­{cut}<1;  kre­yednrmn f$ep>on  0}sbhnlrso  u ade_tp{ sy"eso ,$[ESO]>D  a tpe.n;  a Ssin" _SSIN­I,"hssopd"}fr(.1){ o 1.0   PE:eso­cet(   O:Ssin>rae    iln_tts=      niesae >{      _tr  = &ade_tr,       sat  >hnlrsat      iceet= &ade_nrmn,       nrmn >hnlriceet      _tp  = &ade_tp       so   >hnlrso,    }       )   ;}PE:enl>u(; O:Kre­rn)
  29. 29. MORE CONDENSEDuePE s O;fr(.1){ o 1.0   m cut=0   y$on  ;  PE:eso­cet(   O:Ssin>rae    iln_tts=      niesae >{      _tr  = u        sat  >sb{        sy"eso ,$[ESO]>D  a tre"         a Ssin" _SSIN­I,"hssatd;        PE:enl>il(iceet)         O:Kre­yednrmn;      }       ,      iceet= u        nrmn >sb{        PE:enl>il(iceet)i +cut<1;         O:Kre­yednrmn f+$on  0        sy"eso ,$[ESO]>D  one o$on"         a Ssin" _SSIN­I,"cutdt cut;      }       ,      _tp  = u        so   >sb{        sy"eso ,$[ESO]>D  a tpe"         a Ssin" _SSIN­I,"hssopd;      }       ,    }       )   ;}PE:enl>u(; O:Kre­rn)
  30. 30. REFLEXWritten by author of POE"How POE would be written if I had Moose back then"Stresses composability, reusabilityIncludes plenty of additional roles
  31. 31. USING MOOSE{  pcaeAp   akg p;  ueMoe   s os;  etnselx:ae;   xed Rfe:Bs  ueRfe:Itra;   s elx:nevl  ueRfe:Tat:ace wths;   s elx:ri:Wthdace   wthstce >(   ace ikr=     ia = Rfe:Itra     s  >elx:nevl,    stp=  nevl= ,at_eet=  ,     eu >{itra >1 uorpa >1}  )   ;   sbo_ikrtc    u ntce_ik{    syika ,saa(oatm) ..;     a tc t clrlclie,.  }   } ei p­nw)>u_l(; xtAp>e(­rnal)
  32. 32. COMPLEX? TRY THIS...ueRfe:Itra; s elx:nevl m t=Rfe:Itra­nw y$  elx:nevl>e(  itra  = ,   nevl  >1  at_eet= ,   uorpa >1  o_ik  = u  a tmrtce}   ntc   >sb{syie ikd ,); $­rnal) t>u_l(;
  33. 33. ANYEVENTAnyEvent is very thin and very fastWas written to support as many loops as possible... that conform to the authors rules of compatibilityDoes not use a loop handler, simply runsDoes use conditional variablesHas AE for faster performance
  34. 34. ueAyvn; s nEetm cut=0 y$on  ;m c  =Ayvn­cnvr y$v   nEet>oda;m t  =Ayvn­tmr y$    nEet>ie(  atr  >2   fe  = ,  itra >06   nevl= .,  c    >sb{   b   = u     syh iei o: nEet>o;     a Tetm snw ,Ayvn­nw    +$on =1 n c­sn;     +cut= 0ad$v>ed  }   ,);$v>ev c­rc;
  35. 35. m w w=Ayvn­i( y$;$  nEet>o  f  >SDN   h = *TI,  pl >   ol= r,  c  >sb{   b = u     cop(m ipt= ;     hm  y$nu  )    sy"ed ipt;     a ra:$nu"    udf$;     ne w  }   ,);
  36. 36. USAGES OF ASYNCHRONOUS CODE GUI Services (HTTP/SMTP/etc.) Performance Off-loading tasks
  37. 37. THANK YOU
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×