\dVOdZddlZddlZddlZddlZddlZddlZddlmZm Z m Z m Z m Z m Z mZmZmZmZmZmZmZdZejeedee e eeeehZ en #e$riZYnwxYwdZGdd eZeee fZ!d Z"d Z#d Z$d Z%ddZ&ddZ'e'Z(d dZ)GddZ*Gdde*Z+dZ,d!dZ-ej.dkrGddZ/Gdde*Z0dSdS)"aBasic 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. N) EALREADY EINPROGRESS EWOULDBLOCK ECONNRESETEINVALENOTCONN ESHUTDOWNEISCONNEBADF ECONNABORTEDEPIPEEAGAIN errorcodeznThe {name} module is deprecated and will be removed in Python {remove}. The recommended replacement is asyncio) )removec tj|S#tttf$r |t vrt |cYSd|zcYSwxYw)NzUnknown error %s)osstrerror ValueError OverflowError NameErrorr)errs ..\python\lib\asyncore.py _strerrorrIse'{3  y 1''' )  S> ! ! !!3&&&&'s-A A  A ceZdZdS)ExitNowN)__name__ __module__ __qualname__rrrQsDr"rc| |dS#t$r|YdSxYwN)handle_read_event_reraised_exceptions handle_errorobjs rreadr*VX     ;c| |dS#t$r|YdSxYwr$)handle_write_eventr&r'r(s rwriter/^sX        r,c| |dS#t$r|YdSxYwr$)handle_expt_eventr&r'r(s r _exceptionr2fr+r,cD |tjzr||tjzr||tjzr||tjtjztj zzr| dSdS#t$rH}|j tvr|n| Yd}~dSYd}~dSd}~wt$r|YdSxYwr$)selectPOLLINr%POLLOUTr.POLLPRIr1POLLHUPPOLLERRPOLLNVAL handle_closeOSErrorerrno _DISCONNECTEDr'r&)r)flagses r readwriterAnsT 6=  $  ! ! # # # 6> ! %  " " $ $ $ 6> ! $  ! ! # # # FNV^3foE F             7- ' '                            sB&B,, D67C99#Dc|t}|rog}g}g}t|D]{\}}|}|}|r|||r|js|||s|r|||g|cxkr |cxkr|krnntj|dStj ||||\}}}|D])}| |}|t|*|D])}| |}|t|*|D]+}| |}|t|*dSdSr$) socket_maplistitemsreadablewritableappend acceptingtimesleepr4getr*r/r2) timeoutmaprwr@fdr)is_ris_ws rpollrUs { " BBCIIKK((  GB<<>>D<<>>D   CM   t      a    1      Jw    F-1a111a  B''"++C{ IIII  B''"++C{ #JJJJ  B''"++C{ sOOOOE""<  r"c.|t}|t|dz}tj}|rt |D]y\}}d}|r|tjtjzz}| r|j s|tj z}|r| ||z||}|D]/\}}| |}|t||.dSdS)Nir)rDintr4rUrErFrGr5r7rHrJr6registerrMrA)rNrOpollsterrRr)r?rPs rpoll2rZs' {gdl##{}}H "CIIKK(( - -GBE||~~ 877||~~ (cm (' -!!"e,,, MM' " " " "IB''"++C{ c5 ! ! ! !!"" " "r">@Fc|t}|rttdrt}nt}||r||||dSdS|r!|dkr||||dz }|r |dkdSdSdSdS)NrUr)rDhasattrr4rZrU)rNuse_pollrOcountpoll_funs rlooprbs {GFF++ } # HWc " " " # # # # # eaii HWc " " "AIE eaiiii    iir"ceZdZdZdZdZdZdZdZe dhZ d#dZ dZ d$dZ d$dZejejfdZd$d Zd Zd Zd Zd ZdZdZdZdZdZdZdZd%dZdZ dZ!dZ"dZ#dZ$dZ%dZ&dZ'dZ(d Z)d!Z*d"Z+dS)& dispatcherFNwarningc| t|_n||_d|_|r|d|||d|_ ||_dS#t$r>}|j ttfvrd|_n| |Yd}~dSd}~wwxYwd|_ dS)NFT)rD_map_fileno setblocking set_socket connected getpeernameaddrr<r=rr del_channelsocket)selfsockrOrs r__init__zdispatcher.__init__s ;"DIIDI     U # # # OOD# & & &!DN  ,,..    96 222&+DNN $$S))) #NNNNN DKKKsA.. B683B11B6c|jjdz|jjzg}|jr|jr|dn|jr|d|jV |d|jzn7#t$r*|t|jYnwxYwdd |t|fzS)N. listeningrkz%s:%dz <%s at %#x> ) __class__rr rJrmrIrk TypeErrorreprjoinid)rpstatuss r__repr__zdispatcher.__repr__s.+C/0KKL > 'di ' MM+ & & & & ^ ' MM+ & & & 9  / g 12222 / / / d49oo..... / 0 0"T((;;;s&B1B87B8c,||j}|||j<dSr$)rgrh)rprOs r add_channelzdispatcher.add_channels ;)C DLr"cB|j}||j}||vr||=d|_dSr$)rhrg)rprOrRs rrnzdispatcher.del_channels0 \ ;)C 99B r"c||f|_tj||}|d||dSNF)family_and_typerorirj)rpfamilytyperqs r create_socketzdispatcher.create_socketsK%t|}VT**  r"cp||_||_||dSr$)rofilenorhrrprqrOs rrjzdispatcher.set_socket#s2 {{}}  r"c |jtjtj|jtjtjdzdS#t $rYdSwxYw)Nr])ro setsockopt SOL_SOCKET SO_REUSEADDR getsockoptr<rps rset_reuse_addrzdispatcher.set_reuse_addr(s  K " "!6#6 &&v'8'-':<<>?@          DD sA A$$ A21A2cdSNTr!rs rrGzdispatcher.readable9tr"cdSrr!rs rrHzdispatcher.writable<rr"ctd|_tjdkr|dkrd}|j|S)NTnt)rJrnamerolisten)rpnums rrzdispatcher.listenCs7 7d??sQwwC{!!#&&&r"cD||_|j|Sr$)rmrobind)rprms rrzdispatcher.bindIs {%%%r"cLd|_d|_|j|}|tt t fvs|tkrtj dkr ||_ dS|dtfvr||_ | dSt|t|)NFTrr)rk connectingro connect_exrrrrrrrmr handle_connect_eventr<r)rpaddressrs rconnectzdispatcher.connectMsk$$W-- ;+6 6 6 &==RW__DI F 1g,  DI  % % ' ' ' ' '#y~.. .r"c |j\}}||fS#t$rYdSt$r'}|jt t tfvrYd}~dSd}~wwxYwr$)roacceptrxr<r=rr r)rpconnrmwhys rrzdispatcher.accept[s ++--JD$:    44   y[,???ttttt  s" A AAAAc |j|}|S#t$rD}|jtkrYd}~dS|jt vr|Yd}~dSd}~wwxYwNr)rosendr<r=rr>r;)rpdataresultrs rrzdispatcher.sendis [%%d++FM   yK''qqqqqm++!!###qqqqq s A,A'"A'&A''A,c |j|}|s|dS|S#t$r.}|jt vr|Yd}~dSd}~wwxYwNr")rorecvr;r<r=r>)rp buffer_sizerrs rrzdispatcher.recvvs ;##K00D !!###s    yM))!!###sssss  s066 A."A)(A))A.cd|_d|_d|_||jJ |jdS#t $r!}|jttfvrYd}~dSd}~wwxYwdSr) rkrJrrnrocloser<r=rr )rprs rrzdispatcher.closes  ; "  !!#####   9Xu$555655555  # "sA A8A33A8cdtjdt|zdS)Nzlog: %s )sysstderrr/str)rpmessages rlogzdispatcher.logs) s7||344444r"infocD||jvrt|d|dSdS)Nz: )ignore_log_typesprint)rprrs rlog_infozdispatcher.log_infos7 t, , , dddGG, - - - - - - ,r"c|jr|dS|js1|jr||dS|dSr$)rJ handle_acceptrkrr handle_readrs rr%zdispatcher.handle_read_events| >       ,))+++                r"c|jtjtj}|dkrt |t ||d|_d|_dS)NrTF) rorrSO_ERRORr<rhandle_connectrkrrprs rrzdispatcher.handle_connect_eventsak$$V%6HH !88#y~~.. . r"c|jrdS|js|jr||dSr$)rJrkrr handle_writers rr.zdispatcher.handle_write_eventsQ >  F~ , ,))+++ r"c|jtjtj}|dkr|dS|dSr)rorrrr; handle_exptrs rr1zdispatcher.handle_expt_events[k$$V%6HH !88                r"c t\}}}} t|}n#dt|z}YnxYw|d|d|d|d|d d|dS)Nz)<__repr__(self) failed for object at %0x>z-uncaptured python exception, closing channel z (:rv)error)compact_tracebackryr{rr;)rpniltvtbinfo self_reprs rr'zdispatcher.handle_errors-//Q6 OT II OCbhhNIII         s%;c2|dddS)Nz!unhandled incoming priority eventrerrs rrzdispatcher.handle_expts 99EEEEEr"c2|dddS)Nzunhandled read eventrerrs rrzdispatcher.handle_reads ,i88888r"c2|dddS)Nzunhandled write eventrerrs rrzdispatcher.handle_writes -y99999r"c2|dddS)Nzunhandled connect eventrerrs rrzdispatcher.handle_connects /;;;;;r"cJ|}| |j|dSdSr$)rhandle_accepted)rppairs rrzdispatcher.handle_accepts3{{}}   D $ ' ' ' '  r"cZ||dddS)Nzunhandled accepted eventre)rr)rprqrms rrzdispatcher.handle_accepteds*  0)<<<<8J    "''' &&& / / /      $    555....            (FFF999:::<<<((( ===r"rdc.eZdZddZdZdZdZdZdS)dispatcher_with_sendNcLt|||d|_dSr)rdrr out_bufferrs rrrzdispatcher_with_send.__init__s%D$,,,r"cd}t||jdd}|j|d|_dS)Nri)rdrr)rpnum_sents r initiate_sendz"dispatcher_with_send.initiate_sends;??4%)@AA/())4r"c.|dSr$)rrs rrz!dispatcher_with_send.handle_write s r"c:|j pt|jSr$)rklenrrs rrHzdispatcher_with_send.writable sN";s4?';';;r"c|jr%|dt|z|j|z|_|dS)Nz sending %s)rrryrr)rprs rrzdispatcher_with_send.sendsO : 5 MM,d3 4 4 4/D0 r"r)rrr rrrrrHrr!r"rrrsd555 <<<r"rcftj\}}}g}|std|rQ||jjj|jjjt|j f|j }|Q~|d\}}}d d|D}|||f|||fS)Nztraceback does not existrvcg|]}d|zS)z [%s|%s|%s]r!).0xs r z%compact_traceback..+s666!\A%666r") rexc_infoAssertionErrorrItb_framef_code co_filenameco_namer tb_linenotb_nextrz)rrtbrfilefunctionliners rrrs|~~HAq" F 97888  K  * K  &       Z  !":D(D 8866v666 7 7D (D !1a --r"c&|t}t|D]Q} |#t$r}|jt krn|sYd}~;d}~wt$r|sYOxYw|dSr$) rDrEvaluesrr<r=r r&clear)rO ignore_allrs r close_allr.s { #**,,      GGIIII   w% #        IIKKKKKsA A: A&&A:posixcBeZdZdZdZdZdZd dZeZeZ dZ dZ dS) file_wrapperc8tj||_dSr$)rduprRrprRs rrrzfile_wrapper.__init__SsfRjjDGGGr"c|jdkrtjd|zt||dS)Nrzunclosed file %r)source)rRwarningswarnResourceWarningrrs r__del__zfile_wrapper.__del__VsDw!|| 047%)++++ JJLLLLLr"c0tj|jg|RSr$)rr*rRrpargss rrzfile_wrapper.recv\s747*T*** *r"c0tj|jg|RSr$)rr/rRrs rrzfile_wrapper.send_s8DG+d+++ +r"Nch|tjkr|tjkr|sdStd)Nrz-Only asyncore specific behaviour implemented.)rorrNotImplementedError)rpleveloptnamebuflens rrzfile_wrapper.getsockoptbsA***6?**+q%'566 6r"cd|jdkrdS|j}d|_tj|dS)Nrr)rRrrr s rrzfile_wrapper.closems1w{{BDG HRLLLLLr"c|jSr$)rRrs rrzfile_wrapper.filenots 7Nr"r$) rrr rrrrrrr*r/rrr!r"rrrNs  ! ! !     + + + , , , 6 6 6 6        r"rceZdZddZdZdS)file_dispatcherNct|d|d|_ |}n#t$rYnwxYw||t j|ddS)NTF)rdrrrkrAttributeErrorset_filer set_blocking)rprRrOs rrrzfile_dispatcher.__init__ys   dC 0 0 0!DN YY[[!     MM"    OB & & & & &s: AAct||_|j|_|dSr$)rrorrhrr s rrzfile_dispatcher.set_files>&r**DK;--//DL        r"r$)rrr rrrr!r"rrrws7 ' ' ' '     r"r)rBN)r[FNNr)1__doc__r4rorrKr rr=rrrrrrr r r r r rr_DEPRECATION_MSG _deprecatedrrr>rDrr ExceptionrKeyboardInterrupt SystemExitr&r*r/r2rArUrZpoll3rbrdrrrrrrr!r"rr)s8*  NX/@@@@ :xL% "## JJJJJ'''     i    !2J?(%%%%N""""4 $aaaaaaaaP :6...(>7g''''''''R*Us A##A-,A-