ó õùPc@sÙdZddlZddlZddlZddlZddlZddlZddlmZm Z m Z m Z m Z m Z mZmZmZmZmZmZyeWnek rÁiZnXd„Zdefd„ƒYZeeefZd„Zd„Zd „Zd „Zd e d „Z!d e d „Z"e"Z#de$e e d„Z%dfd„ƒYZ&de&fd„ƒYZ'd„Z(e e$d„Z)ej*dkrÕddl+Z+dfd„ƒYZ,de&fd„ƒYZ-ndS(s Basic infrastructure for asynchronous socket service clients and servers. There are only two ways to have a program on a single processor do "more than one thing at a time". Multi-threaded programming is the simplest and most popular way to do it, but there is another very different technique, that lets you have nearly all the advantages of multi-threading, without actually using multiple threads. it's really only practical if your program is largely I/O bound. If your program is CPU bound, then pre-emptive scheduled threads are probably what you really need. Network servers are rarely CPU-bound, however. If your operating system supports the select() system call in its I/O library (and nearly all do), then you can use it to juggle multiple communication channels at once; doing other work while your I/O is taking place in the "background." Although this strategy can seem strange and complex, especially at first, it is in many ways easier to understand and control than multi-threaded programming. The module documented here solves many of the difficult problems for you, making the task of building sophisticated high-performance network servers and clients a snap. iÿÿÿÿN( tEALREADYt EINPROGRESSt EWOULDBLOCKt ECONNRESETtEINVALtENOTCONNt ESHUTDOWNtEINTRtEISCONNtEBADFt ECONNABORTEDt errorcodecCsKytj|ƒSWn3tttfk rF|tkr>t|Sd|SXdS(NsUnknown error %s(toststrerrort ValueErrort OverflowErrort NameErrorR (terr((s..\python\lib\asyncore.pyt _strerror@s  tExitNowcBseZRS((t__name__t __module__(((s..\python\lib\asyncore.pyRHscCs9y|jƒWn$tk r$‚n|jƒnXdS(N(thandle_read_eventt_reraised_exceptionst handle_error(tobj((s..\python\lib\asyncore.pytreadMs  cCs9y|jƒWn$tk r$‚n|jƒnXdS(N(thandle_write_eventRR(R((s..\python\lib\asyncore.pytwriteUs  cCs9y|jƒWn$tk r$‚n|jƒnXdS(N(thandle_expt_eventRR(R((s..\python\lib\asyncore.pyt _exception]s  cCsóyz|tj@r|jƒn|tj@r7|jƒn|tj@rQ|jƒn|tjtjBtj B@ry|j ƒnWnrt j k rË}|j dtttttfkr¾|jƒqï|j ƒn$tk rÞ‚n|jƒnXdS(Ni(tselecttPOLLINRtPOLLOUTRtPOLLPRIRtPOLLHUPtPOLLERRtPOLLNVALt handle_closetsocketterrortargsR RRRR RR(Rtflagste((s..\python\lib\asyncore.pyt readwritees"      "   gc Csø|dkrt}n|rôg}g}g}xz|jƒD]l\}}|jƒ}|jƒ}|rt|j|ƒn|rŠ|j|ƒn|s–|r:|j|ƒq:q:Wg|koÌ|koÌ|knrâtj|ƒdSy%tj||||ƒ\}}}Wn3tj k r<} | j dt kr5‚q=dSnXx9|D]1}|j |ƒ}|dkrkqDnt |ƒqDWx9|D]1}|j |ƒ}|dkr§q€nt|ƒq€Wx<|D]1}|j |ƒ}|dkrãq¼nt|ƒq¼WndS(Ni(tNonet socket_maptitemstreadabletwritabletappendttimetsleepRR(R)RtgetRRR( ttimeouttmaptrtwR+tfdRtis_rtis_wR((s..\python\lib\asyncore.pytpollysN     ' %      cCss|dkrt}n|dk r4t|dƒ}ntjƒ}|rox“|jƒD]…\}}d}|jƒrˆ|tjtjBO}n|j ƒr¤|tj O}n|rS|tj tj Btj BO}|j||ƒqSqSWy|j|ƒ}Wn5tjk r&}|jdtkr‚ng}nXxE|D]:\}}|j|ƒ}|dkr[q.nt||ƒq.WndS(Nièi(R-R.tintRR=R/R0R R"R1R!R$R#R%tregisterR(R)RR5R,(R6R7tpollsterR:RR*R8R((s..\python\lib\asyncore.pytpoll2¥s4        g>@cCs–|dkrt}n|r3ttdƒr3t}nt}|dkrbxJ|r^|||ƒqHWn0x-|r‘|dkr‘|||ƒ|d}qeWdS(NR=ii(R-R.thasattrRRAR=(R6tuse_pollR7tcounttpoll_fun((s..\python\lib\asyncore.pytloopÇs      t dispatchercBs[eZeZeZeZeZd Ze dgƒZ d d d„Z d„Z e Z d d„Zd d„Zd„Zd d„Zd„Zd„Zd „Zd „Zd „Zd „Zd „Zd„Zd„Zd„Zd„Zd„Zdd„Zd„Zd„Z d„Z!d„Z"d„Z#d„Z$d„Z%d„Z&d„Z'd„Z(d„Z)RS(!twarningcCsÁ|dkrt|_n ||_d|_|r´|jdƒ|j||ƒt|_y|jƒ|_ Wq½t j k r°}|j dt krt|_q±|j|ƒ‚q½Xn d|_ dS(Ni(R-R.t_mapt_filenot setblockingt set_sockettTruet connectedt getpeernametaddrR'R(R)RtFalset del_channel(tselftsockR7R((s..\python\lib\asyncore.pyt__init__âs          cCsÉ|jjd|jjg}|jr?|jr?|jdƒn|jrX|jdƒn|jdk r¬y|jd|jƒWq¬tk r¨|jt |jƒƒq¬Xnddj |ƒt |ƒfS(Nt.t listeningRNs%s:%ds <%s at %#x>t ( t __class__RRt acceptingRPR2RNR-t TypeErrortreprtjointid(RStstatus((s..\python\lib\asyncore.pyt__repr__s  cCs)|dkr|j}n|||js:uncaptured python exception, closing channel %s (%s:%s %s)R((tcompact_tracebackR\R^R’R&(RStniltttvttbinfot self_repr((s..\python\lib\asyncore.pyRÝscCs|jddƒdS(Ns!unhandled incoming priority eventRH(R’(RS((s..\python\lib\asyncore.pyR˜ñscCs|jddƒdS(Nsunhandled read eventRH(R’(RS((s..\python\lib\asyncore.pyR”ôscCs|jddƒdS(Nsunhandled write eventRH(R’(RS((s..\python\lib\asyncore.pyR—÷scCs|jddƒdS(Nsunhandled connect eventRH(R’(RS((s..\python\lib\asyncore.pyR–úscCs|jddƒdS(Nsunhandled accept eventRH(R’(RS((s..\python\lib\asyncore.pyR“ýscCs|jddƒ|jƒdS(Nsunhandled close eventRH(R’R(RS((s..\python\lib\asyncore.pyR&sN(*RRRQtdebugRNRZtclosingR-RPt frozensetR‘RUR`t__str__RaRRReRLRkR0R1RnRpRuRvRyR}RRŠRR’RRsRRRR˜R”R—R–R“R&(((s..\python\lib\asyncore.pyRGÙsJ                       tdispatcher_with_sendcBs;eZddd„Zd„Zd„Zd„Zd„ZRS(cCs tj|||ƒd|_dS(NR|(RGRUt out_buffer(RSRTR7((s..\python\lib\asyncore.pyRU scCs3d}tj||jd ƒ}|j||_dS(Nii(RGRyR¤(RStnum_sent((s..\python\lib\asyncore.pyt initiate_sendscCs|jƒdS(N(R¦(RS((s..\python\lib\asyncore.pyR—scCs|j pt|jƒS(N(RNtlenR¤(RS((s..\python\lib\asyncore.pyR1scCsA|jr#|jdt|ƒƒn|j||_|jƒdS(Ns sending %s(RŸR’R\R¤R¦(RSRz((s..\python\lib\asyncore.pyRys N(RRR-RUR¦R—R1Ry(((s..\python\lib\asyncore.pyR£ s    c CsÌtjƒ\}}}g}|s0tdƒ‚nxD|rv|j|jjj|jjjt|j ƒfƒ|j }q3W~|d\}}}dj g|D]}d|^qšƒ}|||f|||fS(Nstraceback does not existiÿÿÿÿRXs [%s|%s|%s]( R‹texc_infotAssertionErrorR2ttb_frametf_codet co_filenametco_nameRt tb_linenottb_nextR]( R›RœttbRtfiletfunctiontlinetxR((s..\python\lib\asyncore.pyR™$s    &cCs¥|dkrt}nx|jƒD]q}y|jƒWq"tk rm}|jdtkr^q“|s“‚q“q"tk r€‚q"|s“‚q“q"Xq"W|jƒdS(Ni( R-R.tvaluesRtOSErrorR)R Rtclear(R7t ignore_allR´((s..\python\lib\asyncore.pyt close_all8s     tposixt file_wrappercBsMeZd„Zd„Zd„Zdd„ZeZeZd„Z d„Z RS(cCstj|ƒ|_dS(N(R tdupR:(RSR:((s..\python\lib\asyncore.pyRU_scGstj|j|ŒS(N(R RR:(RSR)((s..\python\lib\asyncore.pyR}bscGstj|j|ŒS(N(R RR:(RSR)((s..\python\lib\asyncore.pyRyescCs9|tjkr)|tjkr)| r)dStdƒ‚dS(Nis-Only asyncore specific behaviour implemented.(R'RhR•tNotImplementedError(RStleveltoptnametbuflen((s..\python\lib\asyncore.pyRjhs cCstj|jƒdS(N(R RR:(RS((s..\python\lib\asyncore.pyRsscCs|jS(N(R:(RS((s..\python\lib\asyncore.pyRfvsN( RRRUR}RyR-RjRRRRf(((s..\python\lib\asyncore.pyR»Zs     tfile_dispatchercBseZdd„Zd„ZRS(cCsŒtj|d|ƒt|_y|jƒ}Wntk r?nX|j|ƒtj|tj dƒ}|t j B}tj|tj |ƒdS(Ni( RGRUR-RMRNRfR„tset_filetfcntltF_GETFLR t O_NONBLOCKtF_SETFL(RSR:R7R*((s..\python\lib\asyncore.pyRU{s    cCs/t|ƒ|_|jjƒ|_|jƒdS(N(R»R'RfRJRa(RSR:((s..\python\lib\asyncore.pyRˆsN(RRR-RURÂ(((s..\python\lib\asyncore.pyRÁys (.t__doc__RR'R‹R3R…R terrnoRRRRRRRRRR R R R.RRt ExceptionRtKeyboardInterruptt SystemExitRRRRR,R-R=RAtpoll3RQRFRGR£R™R¹RmRÃR»RÁ(((s..\python\lib\asyncore.pyt/s>      R       , ÿ1