Asynchronous programming FTW!

download Asynchronous programming FTW!

of 37

  • date post

    08-May-2015
  • Category

    Technology

  • view

    601
  • download

    0

Embed Size (px)

description

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.

Transcript of Asynchronous programming FTW!

  • 1.ASYNCHRONOUSPROGRAMMINGFTW!(SAWYER X)

2. DOES THIS SOUND FAMILIAR? "Hi""Hi, dad" "Got a minute?""Yes" "Im looking for a movieWhere do I start?""Go to the cinema website""Whats the address?" "www.whatchamacallit.whatever""Just a sec..." 3. ............ 4. "Well?""Ah! It loaded! What now?""Click on new movies" "OK..." ........ "Well?""Loading"*sounds of clicks* 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. ILLUSTRATED(USING WWW::MECHANIZE)$eh>e(cnm_r) mcgt$ieaulimcsces){ f($eh>ucs$eh>lc(NwMve mccikeois)imcsces){ f($eh>ucsmmvemcfn_nus.. y@ois=$eh>idipt(.)} } 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 moviesThen pick a movie, and click on it" "Thank you" "Youre welcome" 8. GREAT SUCCESS! 9. CALLBACKSReference to a codePassed as an argumentCan be called by some other codesbtysmtig{ ur_oehnmipthf y$nu=sitmchf y$b=sitmrsloeoeain$nu) y$eut=sm_prto(iptirsl{sauqK f($eut>tts}eO){$b>rsl{cnet}) c($eut>otn} }tysmtigsmadmipt,sb{ r_oehn("oernonu"umcnet=sit y$otnhfsy"ucsflrqetsycnet" aScesueusas$otn!}) 10. ILLUSTRATED, ASYNC(USING WWW::MECHANIZE)$eh>e($ieaulu mcgtcnm_r,sb{$eh>lc(eois,sb{ mccikNwMveu$eh>idipt(..uy@ois=@ mcfn_nus.,sb{mmve_})}) }) 11. WHY IS THIS USEFUL?Allow others to decide if something is good or not 12. mjnuo>e( y$uo=Jnnwhss>[w11112222] ot=q,cek>{ hcs=Fig= Pn>{o_ucs>sb{ nsces=umjn,$otdt_ y($uohs,$aa)=@mi,$osag)=$uo>nlz_igrsl( y($pls,$vjnaayepn_eut$aa>ter} dt{sdr,) $ef>e_oths,$v slsths($otag)} ,o_al=u nfi>sb{mjn,$ot)=@ y($uohs_$ef>eeehs(hs) sldlt_ot$ot} ,} } ) 13. WHY IS THIS USEFUL?Allow others to decide if something is good or notAllow easy stepping 14. Algorithm::Diff::CallbackueAgrtm:if:alakq sloih:Df:Clbcwdf_rasdf_ahsdf_ras ifary(odfml_ebr, @l_aiymmesnwfml_ebr, @e_aiymmessb{pitapohabu,sit} urnHpyteraothf,sb{pitorohabu,sit} urnSryteraothf,)df_ahs ifhse(oddtis %l_eal,nwdtis %e_eal,sb{syothf, uaLs,sit}sb{syandhf, uaGie,sit}sb{ umkybfr,$fe_ y($e,$eoeatr)=@sy"kycagdfobfro$fe" a$ehnerm$eoetatr} ,) 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. THE EVENT LOOPCreate loopRegister eventsRun themLet the user keep registering new events 17. meet.. y%vns=(.)wie(){ hl1frahmeet(kyeet oecy$vnes%vns){$vn( eet>)} } 18. meet%vns=(.) y%vnseet..wie(){ hl1frahmeet(kyeet oecy$vnes%vns){$vn( eet>)} }wie(){ hl1frahmeet(kyeet oecy$vnes%vns){ieet{eet{tm=tm f($vns$vn}ie}) eet{eet{cd(} } }sbadeet{ ud_vnmtm,$oe)=@ y($iecd_$vns$d{tmtm eet{i}ie}=$ie$vns$d{cdcd eet{i}oe}=$oe$d+ i+} 19. EVENT TYPESTimers (one-time, recurring, sleep replacement)I/O (input output, including networking) 20. FUCK TALKLETS GO PROGRAMMING 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. ueI:Ayc:tem sO:sn:SraueI:Ayc:op sO:sn:LomloO:sn:Lonw y$op=I:Ayc:op>e$op>onc( locneths=sm.te.ot, ot>"oeohrhs"srie=24, evc>135sctp=sra okye>tem,o_tem=u., nsra>sb{..}o_eov_ro>sb{ nrsleerr=ude"antrsle$[1" iCnoeov_]} ,o_onc_ro>sb{ ncneterr=ude"antcnet$[]fie_] iCnoonc_0ald$[1"} ,)$op>u lorn 23. o_tem=u nsra>sb{msra_ y($tem)=@$tem>ofgr( sracniueo_ed=u nra>sb{msl,$ufe,$o_ y($efbfrfef)=@wie{bfrf~s^.)/){ hl($$ufe}=/(*n/sy"eevdaln1 aRcieie$"} rtr eun0} ) $tem>rt(Antaiehr srawieniiillnee)$op>d(sra) load$tem} 24. ANOTHER EXAMPLE$prMO:snMe:Ayc:TP elI:AycNt:sn:HTEI:Ayc>d(mhte:Ayc:TP>e O:snady$tp=Nt:sn:HTnw)syfrI:Ayc>wi_l(mp{$tp>e(_) aoO:snaatalahtgt$)}>lsfUL.x itoRstt Oh hai parallel HTTP GET! 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. POEuePE#AtnldsPE:enladPE:eso. sOuoicueO:KrenO:Ssin#..dfnalakus(etsie. .eieclbcssbnxld)..fr(.1){ o1.0PE:esocet( O:Ssin>raeiln_tts= niesae>{_tr=&ade_tr, sat>hnlrsaticeet=&ade_nrmn, nrmn>hnlriceet_tp=&ade_tp so>hnlrso,} ) }PE:enl>u( O:Krern) 27. sbhnlrsat{ uade_trmkre,$epssin)=@[KRE,HA,SSIN] y($enlha,$eso_ENLEPESOsy"eso,$esoI,"hssatd aSsin"ssin>Datre"$ep>on ha{cut}=0$enl>il(iceet) kreyednrmn}sbhnlriceet{ uade_nrmnmkre,$epssin)=@[KRE,HA,SSIN] y($enlha,$eso_ENLEPESOsy"eso,$esoI,"cutdt,+$ep>on aSsin"ssin>Doneo"+ha{cut}$enl>il(iceet)iha{cut}on0}sbhnlrso uade_tp{sy"eso,$[ESO]>Datpe" aSsin"_SSINI,"hssopd} 28. sbhnlrsat{ uade_trmkre,$epssin)=@[KRE,HA,SSIN] y($enlha,$eso_ENLEPESOsy"eso,$esoI,"hssatd aSsin"ssin>Datre"$ep>on ha{cut}=0$enl>il(iceet) kreyednrmn}sbhnlriceet{ uade_nrmnmkre,$epssin)=@[KRE,HA,SSIN] y($enlha,$eso_ENLEPESOsy"eso,$esoI,"cutdt,+$ep>on aSsin"ssin>Doneo"+ha{cut}$enl>il(iceet)iha{cut}on0}sbhnlrso uade_tp{sy"eso,$[ESO]>Datpe.n aSsin"_SSINI,"hssopd"}fr(.1){ o1.0PE:esocet( O:Ssin>raeiln_tts= niesae>{_tr=&ade_tr, sat>hnlrsaticeet=&ade_nrmn, nrmn>hnlriceet_tp=&ade_tp so>hnlrso,} ) }PE:enl>u( O:Krern) 29. MORE CONDENSEDuePE sOfr(.1){ o1.0mcut=0 y$onPE:esocet( O:Ssin>raeiln_tts= niesae>{_tr=u sat>sb{sy"eso,$[ESO]>Datre" aSsin"_SSINI,"hssatdPE:enl>il(iceet) O:Kreyednrmn} ,iceet=u nrmn>sb{PE:enl>il(iceet)i+cutsb{sy"eso,$[ESO]>Datpe" aSsin"_SSINI,"hssopd} ,} ) }PE:enl>u( O:Krern) 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. USING MOOSE{pcaeAp akgpueMoe sosetnselx:ae xedRfe:BsueRfe:Itra selx:nevlueRfe:Tat:acewths selx:ri:Wthdacewthstce>( aceikr=ia=Rfe:Itra s>elx:nevl,stp=nevl=,at_eet=, eu>{itra>1uorpa>1}) sbo_ikrtc untce_ik{syika,saa(oatm).. atctclrlclie,.} }eipnw)>u_l( xtAp>e(rnal) 32. COMPLEX? TRY THIS...ueRfe:Itra selx:nevlmt=Rfe:Itranw y$elx:nevl>e(itra=, nevl>1at_eet=, uorpa>1o_ik=uatmrtce} ntc>sb{syieikd,)$rnal) t>u_l( 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. ueAyvn snEetmcut=0 y$onmc=Ayvncnvr y$vnEet>odamt=Ayvntmr y$nEet>ie(atr>2 fe=,itra>06 nevl=.,c>sb{ b=usyhieio:nEet>o aTetmsnw,Ayvnnw+$on=1ncsn +cut=0ad$v>ed} ,)$v>ev crc 35. mww=Ayvni( y$$nEet>of>SDN h=*TI,pl> ol=r,c>sb{ b=ucop(mipt= hmy$nu)sy"edipt ara:$nu"udf$ new} ,) 36. USAGES OF ASYNCHRONOUS CODE GUI Services (HTTP/SMTP/etc.) Performance Off-loading tasks 37. THANK YOU