zfc@sdZddlZddlZddlZddlZddlZddlZddlmZm Z m Z m Z m Z m Z mZmZmZmZmZmZmZmZee e eeeefZyeWnek riZnXdZdefdYZeeefZdZ dZ!d Z"d Z#d e$d Z%d e$d Z&e&Z'de(e$e$dZ)dfdYZ*de*fdYZ+dZ,e$e(dZ-ej.dkrddl/Z/dfdYZ0de*fdYZ1ndS(sBasic 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. iN(tEALREADYt EINPROGRESSt EWOULDBLOCKt ECONNRESETtEINVALtENOTCONNt ESHUTDOWNtEINTRtEISCONNtEBADFt ECONNABORTEDtEPIPEtEAGAINt errorcodecCsKytj|SWn3tttfk rF|tkr>t|Sd|SXdS(NsUnknown error %s(toststrerrort ValueErrort OverflowErrort NameErrorR (terr((s /usr/lib64/python2.7/asyncore.pyt _strerrorDs  tExitNowcBseZRS((t__name__t __module__(((s /usr/lib64/python2.7/asyncore.pyRLscCs9y|jWn$tk r$n|jnXdS(N(thandle_read_eventt_reraised_exceptionst handle_error(tobj((s /usr/lib64/python2.7/asyncore.pytreadQs  cCs9y|jWn$tk r$n|jnXdS(N(thandle_write_eventRR(R((s /usr/lib64/python2.7/asyncore.pytwriteYs  cCs9y|jWn$tk r$n|jnXdS(N(thandle_expt_eventRR(R((s /usr/lib64/python2.7/asyncore.pyt _exceptionas  cCsyz|tj@r|jn|tj@r7|jn|tj@rQ|jn|tjtjBtj B@ry|j nWnct j k r}|j dtkr|jq|j n$tk rn|jnXdS(Ni(tselecttPOLLINRtPOLLOUTRtPOLLPRIRtPOLLHUPtPOLLERRtPOLLNVALt handle_closetsocketterrortargst _DISCONNECTEDRR(Rtflagste((s /usr/lib64/python2.7/asyncore.pyt readwriteis"         gc Cs|dkrt}n|rg}g}g}x|jD]v\}}|j}|j}|rt|j|n|r|j r|j|n|s|r:|j|q:q:Wg|ko|ko|knrtj|dSy%t j ||||\}}}Wn3t j k rF} | j dt kr?qGdSnXx9|D]1}|j |}|dkruqNnt|qNWx9|D]1}|j |}|dkrqnt|qWx<|D]1}|j |}|dkrqnt|qWndS(Ni(tNonet socket_maptitemstreadabletwritabletappendt acceptingttimetsleepR!R*R+RtgetRRR ( ttimeouttmaptrtwR.tfdRtis_rtis_wR((s /usr/lib64/python2.7/asyncore.pytpoll}sN     ' %      cCs}|dkrt}n|dk r4t|d}ntj}|ryx|jD]\}}d}|jr|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(Nii(R0R1tintR!RAR2R3R"R$R4R6R#R&R%R'tregisterR*R+RR9R/(R:R;tpollsterR>RR-R<R((s /usr/lib64/python2.7/asyncore.pytpoll2s4       g>@cCs|dkrt}n|r3ttdr3t}nt}|dkrbxJ|r^|||qHWn0x-|r|dkr||||d}qeWdS(NRAii(R0R1thasattrR!RERA(R:tuse_pollR;tcounttpoll_fun((s /usr/lib64/python2.7/asyncore.pytloops      t dispatchercBsaeZeZeZeZeZeZd Z e dgZ d d dZ dZ e Zd dZd dZdZd dZdZdZd Zd Zd Zd Zd ZdZdZdZdZdZddZdZ dZ!dZ"dZ#dZ$dZ%dZ&dZ'dZ(dZ)dZ*RS(!twarningcCs|dkrt|_n ||_d|_|r|jd|j||t|_y|j|_ Wqt j k r}|j dt tfkrt|_q|j|qXn d|_ dS(Ni(R0R1t_mapt_filenot setblockingt set_sockettTruet connectedt getpeernametaddrR)R*R+RRtFalset del_channel(tselftsockR;R((s /usr/lib64/python2.7/asyncore.pyt__init__s          cCs|jjd|jjg}|jr?|jr?|jdn|jrX|jdn|jdk ry|jd|jWqtk r|jt |jqXnddj |t |fS(Nt.t listeningRRs%s:%ds <%s at %#x>t ( t __class__RRR6RTR5RRR0t TypeErrortreprtjointid(RWtstatus((s /usr/lib64/python2.7/asyncore.pyt__repr__ s  cCs)|dkr|j}n|||j((s /usr/lib64/python2.7/asyncore.pyRVs      cCs?||f|_tj||}|jd|j|dS(Ni(tfamily_and_typeR)RORP(RWtfamilyttypeRX((s /usr/lib64/python2.7/asyncore.pyt create_socket's cCs)||_|j|_|j|dS(N(R)tfilenoRNRd(RWRXR;((s /usr/lib64/python2.7/asyncore.pyRP-s cCsTy9|jjtjtj|jjtjtjdBWntjk rOnXdS(Ni(R)t setsockoptt SOL_SOCKETt SO_REUSEADDRt getsockoptR*(RW((s /usr/lib64/python2.7/asyncore.pytset_reuse_addr3s  cCstS(N(RQ(RW((s /usr/lib64/python2.7/asyncore.pyR3DscCstS(N(RQ(RW((s /usr/lib64/python2.7/asyncore.pyR4GscCs=t|_tjdkr-|dkr-d}n|jj|S(Ntnti(RQR6RtnameR)tlisten(RWtnum((s /usr/lib64/python2.7/asyncore.pyRqNs  cCs||_|jj|S(N(RTR)tbind(RWRT((s /usr/lib64/python2.7/asyncore.pyRsTs cCst|_t|_|jj|}|tttfksT|t krat j dkra||_ dS|dt fkr||_ |jntj|t|dS(NRotcei(RoRt(RURRRQt connectingR)t connect_exRRRRRRpRTRthandle_connect_eventR*R (RWtaddressR((s /usr/lib64/python2.7/asyncore.pytconnectXs     cCsty|jj\}}WnJtk r-dStjk re}|jdtttfkr_dSn X||fSdS(Ni( R)tacceptR^R0R*R+RR R (RWtconnRTtwhy((s /usr/lib64/python2.7/asyncore.pyRzfs cCsry|jj|}|SWnQtjk rm}|jdtkrFdS|jdtkrg|jdSnXdS(Ni(R)tsendR*R+RR,R((RWtdatatresultR|((s /usr/lib64/python2.7/asyncore.pyR}ts cCsoy.|jj|}|s)|jdS|SWn:tjk rj}|jdtkrd|jdSnXdS(Nti(R)trecvR(R*R+R,(RWt buffer_sizeR~R|((s /usr/lib64/python2.7/asyncore.pyRs  cCsrt|_t|_t|_|jy|jjWn5tjk rm}|jdt t fkrnqnnXdS(Ni( RURRR6RuRVR)tcloseR*R+RR (RWR|((s /usr/lib64/python2.7/asyncore.pyRs    cCsyt|j|}Wn-tk rEtd|jj|fn9Xdi|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(RWRtretattrtmsg((s /usr/lib64/python2.7/asyncore.pyt __getattr__s cCstjjdt|dS(Nslog: %s (tsyststderrRtstr(RWtmessage((s /usr/lib64/python2.7/asyncore.pytlogstinfocCs%||jkr!d||fGHndS(Ns%s: %s(tignore_log_types(RWRRg((s /usr/lib64/python2.7/asyncore.pytlog_infoscCsP|jr|jn6|jsB|jr5|jn|jn |jdS(N(R6t handle_acceptRRRuRwt handle_read(RW((s /usr/lib64/python2.7/asyncore.pyRs      cCsb|jjtjtj}|dkrBtj|t|n|jt|_t |_ dS(Ni( R)RmRktSO_ERRORR*Rthandle_connectRQRRRURu(RWR((s /usr/lib64/python2.7/asyncore.pyRws    cCs=|jr dS|js/|jr/|jq/n|jdS(N(R6RRRuRwt handle_write(RW((s /usr/lib64/python2.7/asyncore.pyRs    cCsB|jjtjtj}|dkr4|jn |jdS(Ni(R)RmRkRR(t handle_expt(RWR((s /usr/lib64/python2.7/asyncore.pyRs  cCsmt\}}}}yt|}Wndt|}nX|jd||||fd|jdS(Ns)<__repr__(self) failed for object at %0x>s:uncaptured python exception, closing channel %s (%s:%s %s)R*(tcompact_tracebackR_RaRR((RWtniltttvttbinfot self_repr((s /usr/lib64/python2.7/asyncore.pyRscCs|jdddS(Ns!unhandled incoming priority eventRL(R(RW((s /usr/lib64/python2.7/asyncore.pyRscCs|jdddS(Nsunhandled read eventRL(R(RW((s /usr/lib64/python2.7/asyncore.pyRscCs|jdddS(Nsunhandled write eventRL(R(RW((s /usr/lib64/python2.7/asyncore.pyRscCs|jdddS(Nsunhandled connect eventRL(R(RW((s /usr/lib64/python2.7/asyncore.pyRscCs|jdddS(Nsunhandled accept eventRL(R(RW((s /usr/lib64/python2.7/asyncore.pyRscCs|jdd|jdS(Nsunhandled close eventRL(RR(RW((s /usr/lib64/python2.7/asyncore.pyR(sN(+RRRUtdebugRRR6RutclosingR0RTt frozensetRRYRct__str__RdRVRhRPRnR3R4RqRsRyRzR}RRRRRRRwRRRRRRRRR((((s /usr/lib64/python2.7/asyncore.pyRKsL                       tdispatcher_with_sendcBs;eZdddZdZdZdZdZRS(cCs tj|||d|_dS(NR(RKRYt out_buffer(RWRXR;((s /usr/lib64/python2.7/asyncore.pyRYscCs3d}tj||jd }|j||_dS(Nii(RKR}R(RWtnum_sent((s /usr/lib64/python2.7/asyncore.pyt initiate_sendscCs|jdS(N(R(RW((s /usr/lib64/python2.7/asyncore.pyRscCs|j pt|jS(N(RRtlenR(RW((s /usr/lib64/python2.7/asyncore.pyR4scCsA|jr#|jdt|n|j||_|jdS(Ns sending %s(RRR_RR(RWR~((s /usr/lib64/python2.7/asyncore.pyR}"s N(RRR0RYRRR4R}(((s /usr/lib64/python2.7/asyncore.pyRs    c Cstj\}}}g}|s0tdnxD|rv|j|jjj|jjjt|j f|j }q3W~|d\}}}dj g|D]}d|^q}|||f|||fS(Nstraceback does not existiR\s [%s|%s|%s]( Rtexc_infotAssertionErrorR5ttb_frametf_codet co_filenametco_nameRt tb_linenottb_nextR`( RRttbRtfiletfunctiontlinetxR((s /usr/lib64/python2.7/asyncore.pyR,s    &cCs|dkrt}nx|jD]q}y|jWq"tk rm}|jdtkr^q|sqq"tk rq"|sqq"Xq"W|jdS(Ni( R0R1tvaluesRtOSErrorR+R Rtclear(R;t ignore_allR((s /usr/lib64/python2.7/asyncore.pyt close_all@s     tposixt file_wrappercBsMeZdZdZdZddZeZeZdZ dZ RS(cCstj||_dS(N(RtdupR>(RWR>((s /usr/lib64/python2.7/asyncore.pyRYgscGstj|j|S(N(RRR>(RWR+((s /usr/lib64/python2.7/asyncore.pyRjscGstj|j|S(N(RRR>(RWR+((s /usr/lib64/python2.7/asyncore.pyR}mscCs9|tjkr)|tjkr)| r)dStddS(Nis-Only asyncore specific behaviour implemented.(R)RkRtNotImplementedError(RWtleveltoptnametbuflen((s /usr/lib64/python2.7/asyncore.pyRmps cCs6|jdkrdS|j}d|_tj|dS(Nii(R>RR(RWR>((s /usr/lib64/python2.7/asyncore.pyR{s   cCs|jS(N(R>(RW((s /usr/lib64/python2.7/asyncore.pyRisN( RRRYRR}R0RmRRRRi(((s /usr/lib64/python2.7/asyncore.pyRbs     tfile_dispatchercBseZddZdZRS(cCstj|d|t|_y|j}Wntk r?nX|j|tj|tj d}|t j B}tj|tj |dS(Ni( RKRYR0RQRRRiRtset_filetfcntltF_GETFLRt O_NONBLOCKtF_SETFL(RWR>R;R-((s /usr/lib64/python2.7/asyncore.pyRYs    cCs/t||_|jj|_|jdS(N(RR)RiRNRd(RWR>((s /usr/lib64/python2.7/asyncore.pyRsN(RRR0RYR(((s /usr/lib64/python2.7/asyncore.pyRs (2t__doc__R!R)RR7RRterrnoRRRRRRRRRR R R R R RR,R1RRt ExceptionRtKeyboardInterruptt SystemExitRRRR R/R0RAREtpoll3RURJRKRRRRpRRR(((s /usr/lib64/python2.7/asyncore.pyt/sB      ^        -!3  #