ó õ^‚Rc@sdZddlZddlZddlZddlZddlZddlZddlmZm Z m Z m Z m Z m Z mZmZmZmZmZmZmZmZee e eeeefƒZyeWnek rëiZnXd„Zdefd„ƒYZeeefZd„Z d„Z!d „Z"d „Z#d 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„ƒYZ0de*fd„ƒYZ1ndS(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 ECONNABORTEDtEPIPEtEAGAINt errorcodecCsKytj|ƒSWn3tttfk rF|tkr>t|Sd|SXdS(NsUnknown error %s(toststrerrort ValueErrort OverflowErrort NameErrorR (terr((s/usr/lib/python2.7/asyncore.pyt _strerrorDs  tExitNowcBseZRS((t__name__t __module__(((s/usr/lib/python2.7/asyncore.pyRLscCs9y|jƒWn$tk r$‚n|jƒnXdS(N(thandle_read_eventt_reraised_exceptionst handle_error(tobj((s/usr/lib/python2.7/asyncore.pytreadQs  cCs9y|jƒWn$tk r$‚n|jƒnXdS(N(thandle_write_eventRR(R((s/usr/lib/python2.7/asyncore.pytwriteYs  cCs9y|jƒWn$tk r$‚n|jƒnXdS(N(thandle_expt_eventRR(R((s/usr/lib/python2.7/asyncore.pyt _exceptionas  cCsäyz|tj@r|jƒn|tj@r7|jƒn|tj@rQ|jƒn|tjtjBtj B@ry|j ƒnWnct j k r¼}|j dtkr¯|jƒqà|j ƒn$tk rÏ‚n|jƒnXdS(Ni(tselecttPOLLINRtPOLLOUTRtPOLLPRIRtPOLLHUPtPOLLERRtPOLLNVALt handle_closetsocketterrortargst _DISCONNECTEDRR(Rtflagste((s/usr/lib/python2.7/asyncore.pyt readwriteis"         gc Cs|dkrt}n|rþg}g}g}x„|jƒD]v\}}|jƒ}|jƒ}|rt|j|ƒn|r”|j r”|j|ƒn|s |r:|j|ƒq:q:Wg|koÖ|koÖ|knrìtj|ƒdSy%t j ||||ƒ\}}}Wn3t j k rF} | j dt kr?‚qGdSnXx9|D]1}|j |ƒ}|dkruqNnt|ƒqNWx9|D]1}|j |ƒ}|dkr±qŠnt|ƒqŠWx<|D]1}|j |ƒ}|dkríqÆnt|ƒqÆWndS(Ni(tNonet socket_maptitemstreadabletwritabletappendt acceptingttimetsleepR!R*R+RtgetRRR ( ttimeouttmaptrtwR.tfdRtis_rtis_wR((s/usr/lib/python2.7/asyncore.pytpoll}sN     ' %      cCs}|dkrt}n|dk r4t|dƒ}ntjƒ}|ryx|jƒD]\}}d}|jƒrˆ|tjtjBO}n|j ƒr®|j r®|tj O}n|rS|tj tj BtjBO}|j||ƒqSqSWy|j|ƒ}Wn5tjk r0}|jdtkr'‚ng}nXxE|D]:\}}|j|ƒ}|dkreq8nt||ƒq8WndS(Nièi(R0R1tintR!RAR2R3R"R$R4R6R#R&R%R'tregisterR*R+RR9R/(R:R;tpollsterR>RR-R<R((s/usr/lib/python2.7/asyncore.pytpoll2ªs4       g>@cCs–|dkrt}n|r3ttdƒr3t}nt}|dkrbxJ|r^|||ƒqHWn0x-|r‘|dkr‘|||ƒ|d}qeWdS(NRAii(R0R1thasattrR!RERA(R:tuse_pollR;tcounttpoll_fun((s/usr/lib/python2.7/asyncore.pytloopÍs      t dispatchercBsaeZeZeZeZeZeZd Z e dgƒZ d d d„Z d„Z e Zd d„Zd d„Zd„Zd d„Zd„Zd„Zd „Zd „Zd „Zd „Zd „Zd„Zd„Zd„Zd„Zd„Zdd„Zd„Z d„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 tfkr£t|_q·|j|ƒ‚qÃXn d|_ dS(Ni(R0R1t_mapt_filenot setblockingt set_sockettTruet connectedt getpeernametaddrR)R*R+RRtFalset del_channel(tselftsockR;R((s/usr/lib/python2.7/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 listeningRRs%s:%ds <%s at %#x>t ( t __class__RRR6RTR5RRR0t TypeErrortreprtjointid(RWtstatus((s/usr/lib/python2.7/asyncore.pyt__repr__ s  cCs)|dkr|j}n|||j((s/usr/lib/python2.7/asyncore.pyRVs      cCs?||f|_tj||ƒ}|jdƒ|j|ƒdS(Ni(tfamily_and_typeR)RORP(RWtfamilyttypeRX((s/usr/lib/python2.7/asyncore.pyt create_socket's cCs)||_|jƒ|_|j|ƒdS(N(R)tfilenoRNRd(RWRXR;((s/usr/lib/python2.7/asyncore.pyRP-s cCsTy9|jjtjtj|jjtjtjƒdBƒWntjk rOnXdS(Ni(R)t setsockoptt SOL_SOCKETt SO_REUSEADDRt getsockoptR*(RW((s/usr/lib/python2.7/asyncore.pytset_reuse_addr3s  cCstS(N(RQ(RW((s/usr/lib/python2.7/asyncore.pyR3DscCstS(N(RQ(RW((s/usr/lib/python2.7/asyncore.pyR4GscCs=t|_tjdkr-|dkr-d}n|jj|ƒS(Ntnti(RQR6RtnameR)tlisten(RWtnum((s/usr/lib/python2.7/asyncore.pyRqNs  cCs||_|jj|ƒS(N(RTR)tbind(RWRT((s/usr/lib/python2.7/asyncore.pyRsTs cCs£t|_t|_|jj|ƒ}|tttfksT|t krat j dkra||_ dS|dt fkr‰||_ |jƒntj|t|ƒ‚dS(NRotcei(sntRt(RURRRQt connectingR)t connect_exRRRRRRpRTRthandle_connect_eventR*R (RWtaddressR((s/usr/lib/python2.7/asyncore.pytconnectXs     cCsty|jjƒ\}}WnJtk r-dStjk re}|jdtttfkr_dS‚n X||fSdS(Ni( R)tacceptR^R0R*R+RR R (RWtconnRTtwhy((s/usr/lib/python2.7/asyncore.pyRzfs cCsry|jj|ƒ}|SWnQtjk rm}|jdtkrFdS|jdtkrg|jƒdS‚nXdS(Ni(R)tsendR*R+RR,R((RWtdatatresultR|((s/usr/lib/python2.7/asyncore.pyR}ts cCsoy.|jj|ƒ}|s)|jƒdS|SWn:tjk rj}|jdtkrd|jƒdS‚nXdS(Nti(R)trecvR(R*R+R,(RWt buffer_sizeR~R|((s/usr/lib/python2.7/asyncore.pyRs  cCsrt|_t|_t|_|jƒy|jjƒWn5tjk rm}|jdt t fkrn‚qnnXdS(Ni( RURRR6RuRVR)tcloseR*R+RR (RWR|((s/usr/lib/python2.7/asyncore.pyRƒ“s    cCs‚yt|j|ƒ}Wn-tk rEtd|jj|fƒ‚n9Xdi|jjd6|d6}tj|tddƒ|SdS(Ns!%s instance has no attribute '%s'sB%(me)s.%(attr)s is deprecated. Use %(me)s.socket.%(attr)s instead.tmetattrt stackleveli(tgetattrR)tAttributeErrorR]RtwarningstwarntDeprecationWarning(RWR…tretattrtmsg((s/usr/lib/python2.7/asyncore.pyt __getattr__ s cCstjjdt|ƒƒdS(Nslog: %s (tsyststderrRtstr(RWtmessage((s/usr/lib/python2.7/asyncore.pytlog°stinfocCs%||jkr!d||fGHndS(Ns%s: %s(tignore_log_types(RWR’Rg((s/usr/lib/python2.7/asyncore.pytlog_info³scCsP|jr|jƒn6|jsB|jr5|jƒn|jƒn |jƒdS(N(R6t handle_acceptRRRuRwt handle_read(RW((s/usr/lib/python2.7/asyncore.pyR·s      cCsb|jjtjtjƒ}|dkrBtj|t|ƒƒ‚n|jƒt|_t |_ dS(Ni( R)RmRktSO_ERRORR*Rthandle_connectRQRRRURu(RWR((s/usr/lib/python2.7/asyncore.pyRwÃs    cCs=|jr dS|js/|jr/|jƒq/n|jƒdS(N(R6RRRuRwt handle_write(RW((s/usr/lib/python2.7/asyncore.pyRËs    cCsB|jjtjtjƒ}|dkr4|jƒn |jƒdS(Ni(R)RmRkR™R(t handle_expt(RWR((s/usr/lib/python2.7/asyncore.pyRÖs  cCsmtƒ\}}}}yt|ƒ}Wndt|ƒ}nX|jd||||fdƒ|jƒdS(Ns)<__repr__(self) failed for object at %0x>s:uncaptured python exception, closing channel %s (%s:%s %s)R*(tcompact_tracebackR_RaR–R((RWtniltttvttbinfot self_repr((s/usr/lib/python2.7/asyncore.pyRåscCs|jddƒdS(Ns!unhandled incoming priority eventRL(R–(RW((s/usr/lib/python2.7/asyncore.pyRœùscCs|jddƒdS(Nsunhandled read eventRL(R–(RW((s/usr/lib/python2.7/asyncore.pyR˜üscCs|jddƒdS(Nsunhandled write eventRL(R–(RW((s/usr/lib/python2.7/asyncore.pyR›ÿscCs|jddƒdS(Nsunhandled connect eventRL(R–(RW((s/usr/lib/python2.7/asyncore.pyRšscCs|jddƒdS(Nsunhandled accept eventRL(R–(RW((s/usr/lib/python2.7/asyncore.pyR—scCs|jddƒ|jƒdS(Nsunhandled close eventRL(R–Rƒ(RW((s/usr/lib/python2.7/asyncore.pyR(sN(+RRRUtdebugRRR6RutclosingR0RTt frozensetR•RYRct__str__RdRVRhRPRnR3R4RqRsRyRzR}RRƒRŽR“R–RRwRRRRœR˜R›RšR—R((((s/usr/lib/python2.7/asyncore.pyRKßsL                       tdispatcher_with_sendcBs;eZddd„Zd„Zd„Zd„Zd„ZRS(cCs tj|||ƒd|_dS(NR€(RKRYt out_buffer(RWRXR;((s/usr/lib/python2.7/asyncore.pyRYscCs3d}tj||jd ƒ}|j||_dS(Nii(RKR}R¨(RWtnum_sent((s/usr/lib/python2.7/asyncore.pyt initiate_sendscCs|jƒdS(N(Rª(RW((s/usr/lib/python2.7/asyncore.pyR›scCs|j pt|jƒS(N(RRtlenR¨(RW((s/usr/lib/python2.7/asyncore.pyR4scCsA|jr#|jdt|ƒƒn|j||_|jƒdS(Ns sending %s(R£R–R_R¨Rª(RWR~((s/usr/lib/python2.7/asyncore.pyR}"s N(RRR0RYRªR›R4R}(((s/usr/lib/python2.7/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ÿÿÿÿR\s [%s|%s|%s]( Rtexc_infotAssertionErrorR5ttb_frametf_codet co_filenametco_nameR‘t tb_linenottb_nextR`( RŸR ttbR¡tfiletfunctiontlinetxR”((s/usr/lib/python2.7/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( R0R1tvaluesRƒtOSErrorR+R Rtclear(R;t ignore_allR¸((s/usr/lib/python2.7/asyncore.pyt close_all@s     tposixt file_wrappercBsMeZd„Zd„Zd„Zdd„ZeZeZd„Z d„Z RS(cCstj|ƒ|_dS(N(RtdupR>(RWR>((s/usr/lib/python2.7/asyncore.pyRYgscGstj|j|ŒS(N(RRR>(RWR+((s/usr/lib/python2.7/asyncore.pyRjscGstj|j|ŒS(N(RRR>(RWR+((s/usr/lib/python2.7/asyncore.pyR}mscCs9|tjkr)|tjkr)| r)dStdƒ‚dS(Nis-Only asyncore specific behaviour implemented.(R)RkR™tNotImplementedError(RWtleveltoptnametbuflen((s/usr/lib/python2.7/asyncore.pyRmps cCstj|jƒdS(N(RRƒR>(RW((s/usr/lib/python2.7/asyncore.pyRƒ{scCs|jS(N(R>(RW((s/usr/lib/python2.7/asyncore.pyRi~sN( RRRYRR}R0RmRRRƒRi(((s/usr/lib/python2.7/asyncore.pyR¿bs     tfile_dispatchercBseZdd„Zd„ZRS(cCsŒtj|d|ƒt|_y|jƒ}Wntk r?nX|j|ƒtj|tj dƒ}|t j B}tj|tj |ƒdS(Ni( RKRYR0RQRRRiRˆtset_filetfcntltF_GETFLRt O_NONBLOCKtF_SETFL(RWR>R;R-((s/usr/lib/python2.7/asyncore.pyRYƒs    cCs/t|ƒ|_|jjƒ|_|jƒdS(N(R¿R)RiRNRd(RWR>((s/usr/lib/python2.7/asyncore.pyRÆsN(RRR0RYRÆ(((s/usr/lib/python2.7/asyncore.pyRÅs (2t__doc__R!R)RR7R‰RterrnoRRRRRRRRRR R R R R R¥R,R1RRt ExceptionRtKeyboardInterruptt SystemExitRRRR R/R0RAREtpoll3RURJRKR§RR½RpRÇR¿RÅ(((s/usr/lib/python2.7/asyncore.pyt/sB      ^        -!ÿ3