This document discusses asynchronous programming and provides examples using various asynchronous programming models in Perl, including IO::Async, POE, and callbacks. It illustrates how asynchronous programming allows for control by other processes, asynchronous code, and event-driven programming. Examples show setting up asynchronous HTTP requests using IO::Async and running sessions and events in POE.
2. DOES THIS SOUND FAMILIAR?
"Hi"
"Hi, dad"
"Got a minute?"
"Yes"
"I'm looking for a movie
Where do I start?"
"Go to the cinema website"
"What's the address?"
"www.whatchamacallit.whatever"
"Just a sec..."
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, you'll have instructions"
* eternity passes by *
"WELL?!?"
"Just a sec... it's loading..."
/me shoots myself
6. ILLUSTRATED
(USING WWW::MECHANIZE)
$eh>e(cnm_r)
mcgt$ieaul;
i mcsces){
f($eh>ucs
$eh>lc(NwMve';
mccik'e ois)
i mcsces){
f($eh>ucs
m mve mcfn_nus..
y@ois=$eh>idipt(.)
}
}
7. LET'S TRY THIS AGAIN...
"Hi"
"Hi, dad"
"Got a minute?"
"Yes"
"I'm 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"
"You're welcome"
9. CALLBACKS
Reference to a code
Passed as an argument
Can be called by some other code
sbtysmtig{
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' q'K
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. ILLUSTRATED, ASYNC
(USING WWW::MECHANIZE)
$eh>e($ieaul u
mcgt cnm_r,sb{
$eh>lc('e ois,sb{
mccik NwMve' u
$eh>idipt(.. u y@ois=@
mcfn_nus .,sb{m mve _})
})
;
})
;
11. WHY IS THIS USEFUL?
Allow others to decide if something is good or not
12. m jn uo>e(
y$uo=Jnnw
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 jnaayepn_eut
$aa>'ter}
dt{sdr',
)
;
$ef>e_ot hs,$v ;
slsths($ot ag)
}
,
o_al= u
nfi >sb{
m jn,$ot)=@;
y($uo hs _
$ef>eeehs(hs)
sldlt_ot$ot;
}
,
}
}
);
13. WHY IS THIS USEFUL?
Allow others to decide if something is good or not
Allow easy stepping
15. WHY IS THIS USEFUL?
Allow others to decide if something is good or not
Allow easy stepping
Allow control by other process (a type of IOC)
Allow asynchronous code
16. THE EVENT LOOP
Create loop
Register events
Run them
Let the user keep registering new events
25. POE
Loop interface: POE::Kernel
Runs "sessions" (POE::Session)
Each session is a context of events
Each session has an ID and heap
Call events in a current or different session
Explicitly call POE::Kernel->run when ready
27. sbhnlrsat{
u ade_tr
m kre,$ep ssin)=@[KRE,HA,SSIN]
y($enl ha,$eso _ ENL EP ESO ;
sy"eso ,$esoI,"hssatd;
a Ssin" ssin>D a tre"
$ep>'on' ;
ha{cut}=0
$enl>il(iceet)
kreyed'nrmn';
}
sbhnlriceet{
u ade_nrmn
m kre,$ep ssin)=@[KRE,HA,SSIN]
y($enl ha,$eso _ ENL EP ESO ;
sy"eso ,$esoI,"cutdt ,+$ep>'on';
a Ssin" ssin>D one o" +ha{cut}
$enl>il(iceet)i ha{cut}<1;
kreyed'nrmn' f$ep>'on' 0
}
sbhnlrso
u ade_tp{
sy"eso ,$[ESO]>D a tpe"
a Ssin" _SSINI,"hssopd;
}
28. sbhnlrsat{
u ade_tr
m kre,$ep ssin)=@[KRE,HA,SSIN]
y($enl ha,$eso _ ENL EP ESO ;
sy"eso ,$esoI,"hssatd;
a Ssin" ssin>D a tre"
$ep>'on' ;
ha{cut}=0
$enl>il(iceet)
kreyed'nrmn';
}
sbhnlriceet{
u ade_nrmn
m kre,$ep ssin)=@[KRE,HA,SSIN]
y($enl ha,$eso _ ENL EP ESO ;
sy"eso ,$esoI,"cutdt ,+$ep>'on';
a Ssin" ssin>D one o" +ha{cut}
$enl>il(iceet)i ha{cut}<1;
kreyed'nrmn' f$ep>'on' 0
}
sbhnlrso
u ade_tp{
sy"eso ,$[ESO]>D a tpe.n;
a Ssin" _SSINI,"hssopd"
}
fr(.1){
o 1.0
PE:esocet(
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:Krern)
29. MORE CONDENSED
uePE
s O;
fr(.1){
o 1.0
m cut=0
y$on ;
PE:esocet(
O:Ssin>rae
iln_tts=
niesae >{
_tr = u
sat >sb{
sy"eso ,$[ESO]>D a tre"
a Ssin" _SSINI,"hssatd;
PE:enl>il(iceet)
O:Kreyed'nrmn';
}
,
iceet= u
nrmn >sb{
PE:enl>il(iceet)i +cut<1;
O:Kreyed'nrmn' f+$on 0
sy"eso ,$[ESO]>D one o$on"
a Ssin" _SSINI,"cutdt cut;
}
,
_tp = u
so >sb{
sy"eso ,$[ESO]>D a tpe"
a Ssin" _SSINI,"hssopd;
}
,
}
)
;
}
PE:enl>u(;
O:Krern)
30. REFLEX
Written by author of POE
"How POE would be written if I had Moose back then"
Stresses composability, reusability
Includes plenty of additional roles
31. USING MOOSE
{
pcaeAp
akg p;
ueMoe
s os;
etns'elx:ae;
xed Rfe:Bs'
ueRfe:Itra;
s elx:nevl
ueRfe:Tat:ace wths;
s elx:ri:Wthd'ace'
wthstce >(
ace ikr=
ia = Rfe:Itra'
s >'elx:nevl,
stp= nevl= ,at_eet= ,
eu >{itra >1 uorpa >1}
)
;
sbo_ikrtc
u ntce_ik{
sy'ika ,saa(oatm) ..;
a tc t' clrlclie,'.'
}
}
ei pnw)>u_l(;
xtAp>e(rnal)
32. COMPLEX? TRY THIS...
ueRfe:Itra;
s elx:nevl
m t=Rfe:Itranw
y$ elx:nevl>e(
itra = ,
nevl >1
at_eet= ,
uorpa >1
o_ik = u a tmrtce'}
ntc >sb{sy'ie ikd ,
);
$rnal)
t>u_l(;
33. ANYEVENT
AnyEvent is very thin and very fast
Was written to support as many loops as possible
... that conform to the author's rules of compatibility
Does not use a loop handler, simply runs
Does use conditional variables
Has AE for faster performance
34. ueAyvn;
s nEet
m cut=0
y$on ;
m c =Ayvncnvr
y$v nEet>oda;
m t =Ayvntmr
y$ nEet>ie(
atr >2
fe = ,
itra >06
nevl= .,
c >sb{
b = u
sy'h iei o:' nEet>o;
a Tetm snw ,Ayvnnw
+$on =1 n csn;
+cut= 0ad$v>ed
}
,
);
$v>ev
crc;